14. Minimum Depth of Binary Tree
easyAsked at BookingFind the shortest root-to-leaf depth — Booking checks this to test BFS shortest-path intuition transferable to fastest-fallback supplier lookups.
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
0 <= number of 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. DFS recursion
Recurse and take min; handle the one-child case carefully.
- Time
- O(n)
- Space
- O(h)
function minDepth(r){ if(!r) return 0; if(!r.left) return 1+minDepth(r.right); if(!r.right) return 1+minDepth(r.left); return 1+Math.min(minDepth(r.left),minDepth(r.right));}Tradeoff:
2. BFS first leaf
Level-order until you hit the first leaf — short-circuit early.
- Time
- O(n)
- Space
- O(n)
function minDepth(root) {
if (!root) return 0;
let q = [root], d = 1;
while (q.length) {
const nx = [];
for (const n of q) {
if (!n.left && !n.right) return d;
if (n.left) nx.push(n.left);
if (n.right) nx.push(n.right);
}
d++; q = nx;
}
return 0;
}Tradeoff:
Booking-specific tips
Booking values BFS as an early-exit pattern — frame it as finding the fastest fallback supplier in a fanout tree.
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 Booking interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →