14. Minimum Depth of Binary Tree
easyAsked at VercelFind the shortest path from the root to any leaf. Vercel asks this because the recursion has a subtle gotcha that catches candidates who blindly mirror max-depth.
By Sam K., Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2026-Q1)— Vercel asks this specifically for the 'careful with null child' edge case.
- Blind (2025-Q4)— Reported in Vercel onsite recap; the gotcha is the only-child case.
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]5Explanation: All right children; the only leaf is at depth 5.
Approaches
1. Naive 'min + 1' recursion
Mirror max-depth but with Math.min — INCORRECT because a missing child returns 0 and shrinks the answer wrongly.
- Time
- O(n)
- Space
- O(h)
// WRONG
function minDepth(root) {
if (!root) return 0;
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}
// For [1, 2, null], this returns 1 (wrong; correct is 2 because the only leaf is at depth 2).Tradeoff: Tempting but wrong. A null sibling is NOT a leaf; ignoring this gives the wrong answer for skewed shapes.
2. Careful recursion with null-child handling (optimal)
If both children are null, this is a leaf — return 1. If only one child is null, recurse into the non-null child. Otherwise take the min.
- Time
- O(n)
- Space
- O(h)
function minDepth(root) {
if (!root) return 0;
if (!root.left) return 1 + minDepth(root.right);
if (!root.right) return 1 + minDepth(root.left);
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}Tradeoff: The three branches make the leaf-vs-only-child distinction explicit. BFS is an alternative that naturally finds the shallowest leaf first.
Vercel-specific tips
Vercel uses this question SPECIFICALLY to see if you fall into the symmetric-with-max-depth trap. Articulate the leaf definition out loud — 'a leaf has BOTH children null' — before coding. Bonus signal: BFS as the alternative for early termination on the shallowest leaf.
Common mistakes
- Copy-pasting the max-depth shape with Math.min — wrong for skewed trees.
- Defining a leaf as 'a node with no left child' — wrong, leaves require both children null.
- Using BFS but forgetting to early-exit on the first leaf encountered — defeats the purpose.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- BFS version with early termination.
- Maximum depth (LC 104) for contrast.
- Find the leaf with the smallest depth AND its value.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is the symmetric 'min + 1' wrong?
Because a null child returns 0, then Math.min(0, real_height) is 0, and 1 + 0 = 1 — implying the current node is a leaf when it isn't. You must explicitly skip null children.
When is BFS better here?
When the answer is small relative to the tree size; BFS exits as soon as it encounters the first leaf, while DFS explores the whole tree.