Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MARK: - Minimum Depth of Binary Tree
- func minDepth(_ root: TreeNode?) -> Int {
- if root == nil {
- return 0
- }
- if root?.left != nil && root?.right != nil {
- return min(minDepth(root?.left), minDepth(root?.right)) + 1
- }
- if root?.left != nil {
- return minDepth(root?.left) + 1
- }
- return minDepth(root?.right) + 1
- }
- let root = TreeNode(2)
- root.right = TreeNode(3)
- root.right?.right = TreeNode(4)
- root.right?.right?.right = TreeNode(5)
- root.right?.right?.right?.right = TreeNode(6)
- minDepth(root)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement