Skip to main content

15. Minimum Depth of Binary Tree

easyAsked at Reddit

Find the minimum depth (root to nearest leaf). Reddit asks this for two-fold reason: it tests careful recursion (one-sided subtrees are a trap) and connects to early-termination patterns in their comment-tree renderer.

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

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit phone screen for backend roles.

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. Note: 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

Approaches

1. Naive Math.min recursion

Return 1 + min(left, right). Looks symmetric to maxDepth — but is wrong for one-sided subtrees.

Time
O(n)
Space
O(h)
// WRONG: counts null as depth 0 even when there's a sibling leaf.
function minDepth(root) {
  if (!root) return 0;
  return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}

Tradeoff: Returns 1 for root with one child (e.g., root.left=null, root.right={leaf}) because min(0, 1) = 0. Wrong.

2. BFS — first leaf wins (optimal)

BFS level by level. Return depth at the first leaf encountered.

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: BFS short-circuits as soon as the first leaf is found. DFS would have to traverse the whole tree.

Reddit-specific tips

Reddit interviewers explicitly check whether you handle one-sided subtrees. State the gotcha upfront: 'if root has only one child, min depth must go through that child — null isn't a leaf.' Bonus signal: explain why BFS is naturally better than DFS here (first leaf wins).

Common mistakes

  • Using min(left, right) without handling the null-child case.
  • Confusing 'leaf' with 'null child' — a leaf is a node with both children null.
  • Using DFS and traversing the whole tree when BFS would short-circuit.

Follow-up questions

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

  • Maximum depth (LC 104) — symmetric but without the null-child trap.
  • Find leaves of binary tree (LC 366).
  • What if you want all paths to nearest leaf?

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 is BFS better than DFS here?

BFS visits nodes in order of depth, so the first leaf is by definition the closest. DFS may go far down one branch before backtracking.

How do I fix the DFS version?

Special-case: if one child is null and the other isn't, return 1 + the non-null side. Otherwise 1 + min(left, right).

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →