Skip to main content

14. Minimum Depth of Binary Tree

easyAsked at Plaid

Return the minimum depth of a binary tree — the shortest root-to-leaf path. Plaid asks this because finding the nearest leaf in a category tree is exactly how they pick the shallowest classification for new transactions.

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

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • LeetCode Discuss (2026)Plaid SWE I OA — category-tree variant.
  • Glassdoor (2025)Plaid intro.

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. Recursive DFS (naive)

Return 1 + min(left, right).

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

Tradeoff: Buggy — treats a missing child as depth 0, so [1, null, 2] wrongly returns 1. Don't ship this.

2. BFS to first leaf

Level-order traversal. Return depth on the first leaf encountered. BFS finds shallowest by construction.

Time
O(n) worst case, often much less
Space
O(w)
function minDepth(root) {
  if (!root) return 0;
  let level = [root];
  let depth = 1;
  while (level.length) {
    const next = [];
    for (const n of level) {
      if (!n.left && !n.right) return depth;
      if (n.left) next.push(n.left);
      if (n.right) next.push(n.right);
    }
    depth++;
    level = next;
  }
  return depth;
}

Tradeoff: BFS short-circuits at the shallowest leaf — that's the optimal traversal here. DFS would visit all nodes.

Plaid-specific tips

Plaid grades this on whether you spot that BFS is better than DFS for shortest-path-to-leaf. Bonus signal: walk through the [1, null, 2] case explicitly — that's the classic naive bug. Mention how the same BFS pattern shows up when finding the shallowest matching category for a transaction.

Common mistakes

  • Using DFS with naive min — fails when one child is null.
  • Forgetting the leaf check (both children null).
  • Returning depth on a non-leaf node — that's a path of length 1, not depth to leaf.

Follow-up questions

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

  • Shortest path to a node with a specific value.
  • Same problem on an N-ary tree.
  • What if the tree is huge — bidirectional BFS?

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?

For a node with only one child, min(0, depth_of_other) returns 0, which incorrectly treats the missing child as a leaf.

Why BFS over DFS here?

BFS visits shallow nodes first and short-circuits on the first leaf. DFS could walk down a long chain before discovering a shallow leaf on the other side.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →