Skip to content

Commit 4a85b9e

Browse files
committed
Maximum Depth Of Binary Tree
1 parent b14e4d9 commit 4a85b9e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// TC: O(n)
2+
// visiting all nodes
3+
// SC: O(1)
4+
// constant space complexity
5+
class Solution {
6+
public int maxDepth(TreeNode root) {
7+
return getMax(root, 0);
8+
}
9+
10+
private int getMax(TreeNode node, int depth) {
11+
if (node == null) return depth;
12+
depth += 1;
13+
return Math.max(getMax(node.left, depth), getMax(node.right, depth));
14+
}
15+
}

0 commit comments

Comments
 (0)