14. Minimum Depth of Binary Tree
easyAsked at EtsyFind the shallowest leaf — Etsy uses it to test that you can choose BFS for early termination.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given a binary tree, find its minimum depth — the number of nodes along the shortest path from the root down to the nearest leaf.
Constraints
Node count in [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. Naive recursion
Min over both subtrees, but mishandles a single-child case unless guarded.
- Time
- O(n)
- Space
- O(h)
function minDepth(n){
if (!n) return 0;
if (!n.left) return 1 + minDepth(n.right);
if (!n.right) return 1 + minDepth(n.left);
return 1 + Math.min(minDepth(n.left), minDepth(n.right));
}Tradeoff:
2. BFS early stop
Level-order; return depth of the first leaf encountered.
- Time
- O(n)
- Space
- O(n)
function minDepth(root) {
if (!root) return 0;
let 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:
Etsy-specific tips
Etsy will trap candidates who default to DFS for the shortest path — calling out BFS as the right tool is the bonus signal.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Minimum Depth of Binary Tree and other Etsy interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →