Back to DSA
Maximum Depth of Binary Tree
easyGiven the root of a binary tree, compute its maximum depth. The maximum depth is the number of nodes on the longest path from the root down to the most distant leaf.
Examples
Example 1:
Input:
root = [5,3,8,1,4,null,10]Output:
3 Example 2:
Input:
root = [2,null,7]Output:
2Hints
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}Time complexity
O(n)Space complexity
O(h)