Skip to content

Commit 2d67f26

Browse files
committed
Added Max Depth o fBinary Tree
1 parent b8c76b0 commit 2d67f26

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Time: O(n)
3+
* Space: O(d) where d is the depth of the deepest path
4+
*/
5+
class Solution {
6+
public int maxDepth(TreeNode root) {
7+
if (root == null) return 0;
8+
// Find the length of the left and right children paths
9+
int left = maxDepth(root.left);
10+
int right = maxDepth(root.right);
11+
// return the max of left and right length plus 1
12+
return Math.max(left, right) + 1;
13+
}
14+
}

0 commit comments

Comments
 (0)