Maximum Depth of Binary Tree
Given the root of a binary tree, return its maximum depth.
Examples
Input:
root = [3,9,20,null,null,15,7]Output:
3Constraints
š” Hints
Hint 1
A binary tree depth is the maximum of left and right depths + 1.
Algorithm Complexity
Official asymptotic Big-O runtime and auxiliary memory analysis
Linear
Algorithmic Approach
DFS to find depth.
Time ComplexityO(N)
Why:
Depth-First Search (DFS) visits every node in the binary tree exactly once to evaluate max depth.
Space ComplexityO(H)
Why:
The recursion call stack depth equals the height of the tree H (O(log N) average, O(N) worst-case skewed).
Big-O Growth CurveLinear (O(N))
š Dry Run
Solutionā± O(N) | š¾ O(H)
root
3
9
20
15
7
Step 1 / 3