Maximum Depth of Binary Tree
A node's depth is one more than its deeper subtree.
The Call Stack and Its Returns
Problem
Given the root of a binary tree, determine how many nodes appear on the longest path from the root down to any leaf. Return 0 when the tree is empty. You may solve this by recursively combining subtree depths or by counting the levels visited in a breadth-first traversal.
Examples
Input: root = [3,9,20,null,null,15,7]
Output: 3
Input: root = [1,null,2]
Output: 2
The Goal
Find how many levels the tree has: the number of nodes on its longest path from the root down to a leaf. An empty tree has depth 0.
For [3,9,20,null,null,15,7], the longest paths are 3 → 20 → 15 and 3 → 20 → 7, so the depth is 3.
Count nodes, not edges
A single node has depth 1, and an empty tree has depth 0. Count the nodes on the path, not the edges between them.
A leaf is 1 deep.
Key Idea: Ask Each Subtree
A node's depth is one more than the deeper of its two subtrees:
max_depth(node) = 1 + max(max_depth(node.left), max_depth(node.right))
An empty child is 0 deep, which ends the recursion. Each call waits for its left subtree's answer, then its right's, then returns. The answers flow up from the leaves.
A node returns 1 plus the larger of its two depths.
20 returns 1 + max(1, 1) = 2.
The Algorithm
def max_depth(node):
if node is None:
return 0
left = max_depth(node.left)
right = max_depth(node.right)
return 1 + max(left, right)Breadth-first search works too: count the levels a level-order traversal drains. Both visit every node once.
| Approach | How it counts | Extra space |
|---|---|---|
| Recursive DFS | 1 + the deeper subtree | O(h) call stack |
| BFS by levels | One per level drained | O(w) queue, w the widest level |
Walkthrough
Run it on [3,9,20,null,null,15,7].
max_depth(3) calls itself on 9 first. Both of 9's children are None and return 0, so 9 returns 1.
Then 3 waits on 20. Its children, 15 and 7, are leaves that return 1 each, so 20 returns 1 + max(1, 1) = 2.
Back at the root, left = 1 and right = 2, and it returns 3.
Checkpoint · predict the next step
Step 9 of 26 on the stage: max_depth(20): not None, so first find how deep its left subtree goes.
node on 20 · call stack = [max_depth(3) · left 1, max_depth(20)] · 9 returned 1
Pitfalls
- Return 0 for an empty child, not 1, or every depth comes out one too big.
- Take the larger side.
[1,2,null,3,null,4]leans all the way left and is 4 deep. - Count nodes, not edges: a single node is 1 deep.
- A chain of 10,000 nodes recurses 10,000 calls deep, past Python's default limit. Counting levels with a queue avoids that.
What should max_depth(None) return?
Complexity
Each node gets one call, and each call does constant work besides its two recursive calls, so time is O(n).
The call stack holds one call per level of the current path, so space is O(h), where h is the tree's height. That's O(log n) for a balanced tree and O(n) for a chain.
Recursive depth-first search
- Visit each nodeOne call each.dominantO(n)
- Empty childrenn + 1 of them, each returning 0.dominantO(n)
- Call stackOne call per level of the path.O(h)
| n | 10 | 100 | 1,000 | 10,000 |
|---|---|---|---|---|
| O(n) | 10 | 100 | 1K | 10K |
Linear time, and a stack as deep as the tree.
In short
Recurse: the depth of a node is 1 + max(depth(left), depth(right)), with null contributing 0. A BFS level count works too. Both visit every node once.
Common mistake
Off-by-one at the base case — a null child is depth 0, a leaf is depth 1.