15. Minimum Depth of Binary Tree
easyAsked at CoinbaseReturn the minimum depth — the number of nodes along the shortest path from root to nearest leaf. Coinbase asks this to test whether you spot the asymmetric edge case where one child is null.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Coinbase loops.
- Glassdoor (2026-Q1)— Coinbase warm-up.
Problem
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. A leaf is a node with no children.
Constraints
The number of nodes in the tree is in the range [0, 10^5].-1000 <= Node.val <= 1000
Examples
Example 1
root = [3,9,20,null,null,15,7]2Example 2
root = [2,null,3,null,4,null,5,null,6]5Approaches
1. Mirror of maxDepth
Take 1 + min(minDepth(left), minDepth(right)).
- Time
- O(n)
- Space
- O(h)
function minDepth(root) {
if (!root) return 0;
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}Tradeoff: WRONG — when one child is null, min picks 0 and reports the wrong shortest path through the missing branch.
2. BFS first-leaf
Level-order traversal; the first time you dequeue a leaf, return its depth.
- Time
- O(n) worst, often early termination
- Space
- O(w)
function minDepth(root) {
if (!root) return 0;
const q = [[root, 1]];
while (q.length) {
const [n, d] = q.shift();
if (!n.left && !n.right) return d;
if (n.left) q.push([n.left, d + 1]);
if (n.right) q.push([n.right, d + 1]);
}
}Tradeoff: BFS terminates at the first leaf — strictly faster than DFS for shallow leaves. Avoids the one-child-null trap.
Coinbase-specific tips
Coinbase loves this problem because the naive min(left, right) recurrence is WRONG and they want to see if you spot it. When a node has one null child, you must take the non-null branch — not the 0 from null. BFS sidesteps the issue entirely.
Common mistakes
- 1 + min(left, right) — counts a phantom path through the null child.
- Forgetting that a leaf is 'no children', not 'either child null'.
- DFS that doesn't handle the asymmetric case.
Follow-up questions
An interviewer at Coinbase may pivot to one of these next:
- Return the actual path, not just depth.
- Weighted edges — Dijkstra on the tree.
- What if 'leaf' means 'no children OR a specific marker value'?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is the naive recurrence wrong here when it works for maxDepth?
Max ignores 0 in favor of the actual height. Min picks 0 incorrectly, treating a null child as a 0-depth leaf. You must branch: if one child is null, recurse on the other only.
Why does BFS work cleanly?
BFS visits nodes in depth order. The first leaf you dequeue is at the minimum depth by definition — no recurrence trickery needed.
Practice these live with InterviewChamp.AI
Drill Minimum Depth of Binary Tree and other Coinbase interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →