14. Minimum Depth of Binary Tree
easyAsked at LyftFind the minimum depth of a binary tree.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given a binary tree, find its minimum depth — the number of nodes on the shortest path from root to a leaf. A leaf is a node with no children.
Constraints
Number of nodes in range [0, 10^5]-1000 <= node value <= 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 minimum
Recursive minimum of subtree depths with leaf check.
- Time
- O(n)
- Space
- O(h)
function m(n){if(!n) return Infinity; if(!n.left&&!n.right) return 1;
return 1+Math.min(m(n.left),m(n.right));}
return root? m(root):0;Tradeoff:
2. BFS level order
Breadth-first search and return as soon as you find any leaf — guarantees minimum depth.
- Time
- O(n)
- Space
- O(n)
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:
Lyft-specific tips
Lyft prefers BFS here — they argue the early termination is critical when scanning massive partial trees representing live driver positions in a city.
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 Lyft interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →