Skip to content

Commit 6a8e677

Browse files
committed
solve: maximum depth of binary tree
1 parent 62e30cf commit 6a8e677

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// time: O(n) space: O(n)
2+
class Solution {
3+
func maxDepth(_ root: TreeNode?) -> Int {
4+
return dfs(root, 1)
5+
}
6+
7+
func dfs(_ node: TreeNode?, _ n: Int) -> Int {
8+
guard let node = node else { return n - 1 }
9+
return max(dfs(node.left, n+1), dfs(node.right, n+1))
10+
}
11+
}

0 commit comments

Comments
 (0)