Skip to main content

15. Minimum Depth of Binary Tree

easyAsked at Databricks

Find the minimum depth of a binary tree (distance from root to nearest LEAF). Databricks asks this because it tests whether you can distinguish 'null child' from 'leaf' — a subtle case that catches candidates who only memorized max-depth.

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

Source citations

Public interview reports confirming this problem appears in Databricks loops.

  • Glassdoor (2025-09)Databricks runtime team phone screen.
  • LeetCode Discuss (2026-Q1)Asked specifically to catch candidates who copy the max-depth template blindly.

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: Skewed tree — single path of leaves.

Approaches

1. Naive DFS (BROKEN — copy of max-depth template)

Use 1 + min(left, right). Fails because null child returns 0 and gets picked as the 'min'.

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

Tradeoff: WRONG. On [1, 2, null], this returns 1 because min(2, 0) = 0. A leaf isn't a missing child.

2. BFS — first leaf encountered IS the minimum

Level-order traversal. The first leaf (no children) you dequeue is at the minimum depth.

Time
O(n) worst, but early-exits
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: Early-exits on the shallowest leaf. DFS works too if you handle the 'one child null' case explicitly.

Databricks-specific tips

Databricks specifically asks this to see if you fall into the copy-paste trap of using the max-depth template with min substituted. The bonus signal is recognizing the edge case BEFORE coding ('null child isn't a leaf') and choosing BFS for early-exit on shallow leaves. Articulate the trap explicitly — interviewers want to hear you reason about it.

Common mistakes

  • Direct copy of max-depth with min — returns 1 for any tree with a missing child at the root.
  • Treating null as a leaf in the DFS version.
  • Using DFS without the 'one child null' guard, then returning early on the null path.

Follow-up questions

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

  • Fix the DFS: if left is null, depth = 1 + minDepth(right); analogous for right. Only take min if both exist.
  • Find the leaf NODE itself, not just the depth.
  • Why might Databricks care about minimum depth in a query plan? (Hint: shortest path to a leaf scan.)

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 the naive DFS wrong?

A null child returns depth 0. The min picks 0 and you propagate '1' upward, falsely claiming a 1-deep leaf at any node missing a child.

Why BFS over DFS?

BFS finds the shallowest leaf first and stops. DFS visits the whole tree even after finding a leaf, unless you add explicit early-exit logic.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →