Maximum Depth of Binary Tree
Problem Statement
Given the root of a binary tree, return its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to any leaf node.
Input
The root pointer of a binary tree, or null for an empty tree.
Output
An integer representing the maximum number of nodes on any root-to-leaf path.
Constraints
- •
0 <= number of nodes <= 10000 - •
-100 <= Node.val <= 100
Examples
Example 1
root = [3,9,20,null,null,15,7]
3Example 2
root = [1,null,2]
2Example 3
root = []
0Learning Objectives
- Recognise maximum depth as postorder subtree aggregation.
- Use **null -> 0** as the base case.
- Combine child depths with **1 + max(left, right)**.
- Distinguish counting nodes from counting edges.
Intuition
Pattern Recognition
The signal is maximum depth, height, or longest downward root-to-leaf path. This is a traversal where visiting means computing and returning a value for the current subtree, not appending to a list.
A node cannot know its own answer until it knows the best answer from both children. That makes the order postorder: get the left depth, get the right depth, then return 1 + max(left, right). The common trap is returning 1 for null, which overcounts missing children.
Common mistakes
- ×Returning **1** for a **null** child instead of **0**.
- ×Taking **min(left, right)** instead of **max(left, right)**.
- ×Counting edges when the problem asks for nodes on the path.
- ×Using a global variable even though the return value already carries the depth.
Algorithm Explanation
Key idea
Maximum depth introduces subtree aggregation. A null subtree contributes 0. A real node asks both children for their maximum depths and returns 1 + max(leftDepth, rightDepth), where 1 counts the current node.
Recursion walkthrough
Use [3,9,20,null,null,15,7]. Root 3 first asks left child 9. Node 9 sees two null children, gets depths 0 and 0, and returns 1. Then 3 asks right child 20. Node 20 asks 15 and 7; both leaves return 1 for the same reason. Node 20 returns 1 + max(1, 1) = 2. Finally 3 combines left depth 1 and right depth 2, returning 1 + max(1, 2) = 3. This is postorder because every parent returns after its children.
Algorithm
- If root is null, return 0.
- Recursively compute leftDepth from root.left.
- Recursively compute rightDepth from root.right.
- Return 1 + max(leftDepth, rightDepth).
- The value returned by the original root is the maximum depth.
Solutions
Solution: Postorder recursive depth aggregation
Use this as the default solution. It mirrors the definition of maximum depth and sets up harder height-based problems.
Treat each subtree as the same problem. A null subtree returns 0. A non-null node returns one plus the larger depth returned by its two children.
Step-by-step
- If root is null, return 0.
- Compute leftDepth by calling the function on root.left.
- Compute rightDepth by calling the function on root.right.
- Choose the larger child depth.
- Return 1 + max(leftDepth, rightDepth).
O(n)
O(h)
Every node performs one constant-time combine; the recursion stack is O(h), which becomes O(n) for a skewed tree.
Java implementation
Dry Run
Sample input
root = [3,9,20,null,null,15,7]. Track postorder returns from leaves up to the root.
| node | left depth | right depth | returned depth |
|---|---|---|---|
| 9 | 0 | 0 | 1 |
| 15 | 0 | 0 | 1 |
| 7 | 0 | 0 | 1 |
| 20 | 1 | 1 | 2 |
| 3 | 1 | 2 | 3 |
Leaves return 1 because they count themselves above two null depths. The root chooses the deeper right subtree and returns 3.
Interview Tips
Call this a postorder aggregation, not just a traversal. The child calls return complete subtree answers, and the parent combines them. Be precise about null -> 0 and about counting nodes, not edges; that prevents the common off-by-one mistakes.
Likely follow-ups
- How would minimum depth differ when one child is missing?
- How would you check whether the tree is height-balanced?
- How would you compute the diameter using returned child depths?
- Can you compute maximum depth with BFS level order?
Similar Problems
Key Takeaways
- Maximum depth is postorder subtree aggregation.
- The base case is **null -> 0**.
- Each node returns **1 + max(leftDepth, rightDepth)**.
- The same combine template powers balance and diameter problems.