Try it first — give it 15 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
You get the root of a binary tree. Return its maximum depth: the number of nodes on the longest path from the root down to a leaf (a node with no children).
The tree can be empty (depth 0) and has up to 10⁴ nodes.
Example: [3, 9, 20, null, null, 15, 7] has depth 3. The longest path is 3 → 20 → 15 (or 3 → 20 → 7), and it has 3 nodes.
3
/ \
9 20
/ \
15 7Start with brute force
One honest first idea: find every root-to-leaf path, measure each one, and take the longest. You'd walk down from the root, carrying the current length, and record it at every leaf.
That actually works in O(n), because each node is on the way to its leaves only once. So the brute force isn't slow. It's just more machinery than you need: you're tracking a running length and a global best. The cleaner idea flips the direction. Instead of carrying information down, let each subtree send its answer up.
Building the intuition
Stand at the root 3 and pretend you're lazy. You don't want to look at the whole tree. You ask two helpers:
- "Left helper, how deep is the subtree under
9?" → 1. - "Right helper, how deep is the subtree under
20?" → 2.
Now you can answer for the whole tree without looking any further: the root adds one level on top of the deeper side. 1 + max(1, 2) = 3.
How did the helpers answer? The same way. The helper at 20 asked its children (15 and 7, each depth 1) and said 1 + max(1, 1) = 2. The helper at 9 asked two empty subtrees, each depth 0, and said 1 + max(0, 0) = 1.
That's the question this whole stage keeps asking: what does this subtree return to its parent? Here it's simply "my depth". Once you pick that, the code writes itself, and you never have to trace more than one level. Trust that the recursive call returns the right depth. That's the leap of faith.
The optimal approach
- If
rootisnull, return0. An empty tree has no levels. - Ask the left subtree for its depth:
left = maxDepth(root.left). - Ask the right subtree:
right = maxDepth(root.right). - Return
1 + max(left, right).
The subtle detail: the base case is the empty tree, not the leaf. With null → 0, a leaf correctly returns 1 + max(0, 0) = 1, and a node with only one child still works: the missing side says 0, and max picks the real child. If you instead stop at leaves, you have to special-case nodes with one child, and that's where the bugs live.
This is a postorder traversal: the node does its work after both children have reported back.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root is None: # an empty tree has no levels
return 0
# Each child reports how deep its own subtree goes; we add 1 for this node.
left = self.maxDepth(root.left)
right = self.maxDepth(root.right)
return 1 + max(left, right)class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == nullptr) return 0; // an empty tree has no levels
// Each child reports how deep its own subtree goes; we add 1 for this node.
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return 1 + max(left, right);
}
};class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0; // an empty tree has no levels
// Each child reports how deep its own subtree goes; we add 1 for this node.
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return 1 + Math.max(left, right);
}
}/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function (root) {
if (root === null) return 0; // an empty tree has no levels
// Each child reports how deep its own subtree goes; we add 1 for this node.
const left = maxDepth(root.left);
const right = maxDepth(root.right);
return 1 + Math.max(left, right);
};Dry run
Tree [3, 9, 20, null, null, 15, 7], calls listed in the order they finish.
| Call on | left | right | Returns 1 + max |
|---|---|---|---|
| 9 | 0 (null) | 0 (null) | 1 |
| 15 | 0 | 0 | 1 |
| 7 | 0 | 0 | 1 |
| 20 | 1 (from 15) | 1 (from 7) | 2 |
| 3 | 1 (from 9) | 2 (from 20) | 3 |
Complexity
- Time: O(n). Each node is visited exactly once.
- Space: O(h) for the recursion stack, where
his the height. That's O(log n) for a balanced tree and O(n) for a chain.
Edge cases and traps
- Empty tree. Must return 0, not 1. The
null → 0base case handles it. - Counting edges instead of nodes. This problem counts nodes, so a single node has depth 1. Diameter of Binary Tree counts edges. Read which one is asked.
- Using
minby accident or "fixing" one-child nodes. Withnull → 0,maxalready handles a missing child. (The minimum depth problem is where one-child nodes really need care.) - Deep chains. A chain of 10⁴ nodes means 10⁴ stack frames. Plain Python's default recursion limit is 1000, so on your own machine you'd need
sys.setrecursionlimitor the BFS version below.
Say it in the interview
"Each subtree can report its own depth to its parent. An empty tree has depth 0, and any other node is one level on top of its deeper child, so depth is 1 plus the max of the two recursive calls. Every node is visited once, so it's O(n) time, and the recursion stack is O(h), the height of the tree. If the tree could be very deep, I'd switch to BFS and count levels instead."
Follow-ups interviewers ask
- "Do it iteratively." BFS level by level, as in Binary Tree Level Order Traversal: each time you finish one level, add 1. The queue holds at most one level, and no recursion is involved.
- "What about the minimum depth?" Careful: a node with one child isn't a leaf, so you can't just swap
maxformin. If one side is empty, use the other side's depth. BFS is even nicer here, since the first leaf you meet is the answer. - "Check whether the tree is height-balanced." Return the height up the tree, like here, but return −1 as soon as the two children's heights differ by more than 1. That stays O(n).
Practise the same idea
This is Day 52 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free