Skip to main content

14. Minimum Depth of Binary Tree

easyAsked at Zoom

Find the minimum depth from root to the nearest leaf.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from root down to the nearest leaf. A leaf has no children.

Constraints

  • 0 <= nodes <= 10^5
  • -1000 <= Node.val <= 1000

Examples

Example 1

Input
root=[3,9,20,null,null,15,7]
Output
2

Example 2

Input
root=[2,null,3,null,4]
Output
4

Approaches

1. DFS recursion

Recurse both sides; handle single-child specially.

Time
O(n)
Space
O(h)
function min(n){if(!n) return 0; if(!n.left) return 1+min(n.right); if(!n.right) return 1+min(n.left); return 1+Math.min(min(n.left),min(n.right));}

Tradeoff:

2. BFS early exit

Level-order walk; return depth as soon as you hit a leaf.

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:

Zoom-specific tips

Zoom prefers BFS in interviews because it mirrors their lowest-latency-path search in the SFU mesh — explain that bandwidth savings come from early termination.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Minimum Depth of Binary Tree and other Zoom interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →