Skip to main content

14. Minimum Depth of Binary Tree

easyAsked at Asana

Find the minimum depth of a binary tree — the shortest path from root to a leaf. Asana asks this because it's the classic 'recursive trap' where reusing the max-depth template silently breaks.

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

Source citations

Public interview reports confirming this problem appears in Asana loops.

  • Glassdoor (2026-Q1)Asana tree warmup with twist.

Problem

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

Constraints

  • The number of nodes in the tree is in the range [0, 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,null,5,null,6]
Output
5

Explanation: Right-skewed; min path goes all the way to 6.

Approaches

1. Naive min recursion

Mirror max-depth: return 1 + min(left, right). BUG: single-child nodes count their null side as depth 0.

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

Tradeoff: Returns 2 for [2,null,3] instead of 2 — actually 2, but on [2,null,3,null,4] returns 2 instead of 3. The classic mirror-of-max trap.

2. BFS — first leaf wins

BFS level by level. Return depth when you encounter the first leaf.

Time
O(n)
Space
O(w)
function minDepth(root) {
  if (!root) return 0;
  let q = [root], d = 1;
  while (q.length) {
    const next = [];
    for (const n of q) {
      if (!n.left && !n.right) return d;
      if (n.left)  next.push(n.left);
      if (n.right) next.push(n.right);
    }
    q = next; d++;
  }
}

Tradeoff: BFS short-circuits on the first leaf — better worst case than DFS on skewed trees because you don't walk the whole long branch.

Asana-specific tips

Asana cares about whether you spot the asymmetry between max and min depth. Max depth treats null as depth 0 correctly (since the longest path can't include null); min depth doesn't, because the shortest path must end at a leaf. State this trap before coding and you've won half the interview.

Common mistakes

  • Mirroring max-depth as 1 + min(left, right) — wrong on skewed trees.
  • Treating internal nodes with one null child as leaves.
  • Using DFS where BFS would short-circuit faster.

Follow-up questions

An interviewer at Asana may pivot to one of these next:

  • What's the worst-case advantage of BFS vs DFS here?
  • Adapt to find the shallowest leaf with a given value.
  • Generalize to n-ary trees (LC 559 variant).

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why does the naive recursion fail?

A node with one null child returns 0 from that side, then 1 from min(0, ...) = 0 — but that null isn't a leaf. The fix is to skip the null side when only one child exists.

Why does BFS dominate DFS here?

BFS sees leaves in increasing depth order and returns immediately. DFS on a skewed tree might walk the full long branch before finding any leaf.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →