14. Minimum Depth of Binary Tree
easyAsked at RobloxFind the shortest root-to-leaf path length.
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 node. A leaf is a node with no children.
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
Recurse all paths and take min; explores entire tree.
- Time
- O(n)
- Space
- O(h)
const dfs = n => {
if (!n) return Infinity;
if (!n.left && !n.right) return 1;
return 1 + Math.min(dfs(n.left), dfs(n.right));
};Tradeoff:
2. BFS first-leaf
Walk levels and return the 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:
Roblox-specific tips
Roblox prefers the BFS approach since the same level-order pattern shows up when finding the closest visible neighbor in an octree.
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 Roblox interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →