14. Minimum Depth of Binary Tree
easyAsked at DropboxFind the shortest root-to-leaf path length; Dropbox uses it to test BFS-vs-DFS choice for early-termination tree searches in their sync metadata layer.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given the root of a binary tree, return the minimum number of nodes along the shortest root-to-leaf path. A leaf is a node with no children.
Constraints
0 <= nodes <= 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. Full DFS
Recurse both subtrees, take min of leaf depths (with care when one child is null).
- Time
- O(n)
- Space
- O(h)
const f=n=>!n?Infinity:(!n.left&&!n.right?1:1+Math.min(f(n.left),f(n.right)));
return !root?0:f(root);Tradeoff:
2. BFS early exit
Level-order; return depth on first leaf encountered. Better for shallow leaves in tall trees.
- Time
- O(n)
- 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:
Dropbox-specific tips
Dropbox grades on whether you spot the null-child trap — when only one child exists, you must NOT take the min with the missing branch, that's the most common bug.
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 Dropbox interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →