TRT
Sign in
easyCoreLeetCode#breadth-first search#early exit

Minimum depth of binary tree

Find the depth of the shallowest leaf, stopping a level-order traversal at the first leaf it meets.

The Runtime Theory Team1 min read
Taught inLevel-order traversalPro — members onlyVisiting a tree level by level needs a queue instead of a stack. Taking a snapshot of the queue's size separates one level from the next, which solves a family of problems about depth, width and views.
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

inroot = [3, 9, 20, null, null, 15, 7]

out2

inroot = [2, null, 3, null, 4, null, 5, null, 6]

out5

inroot = [1]

out1

Return the number of nodes on the shortest path from the root down to a leaf. A leaf is a node with no children.

Before you code

A recursive 1 + min(left, right) is wrong when one child is missing: a missing child is not a leaf. Breadth-first search avoids the trap and can stop early.

Hints

  1. Traverse level by level, counting levels.
  2. Return the current level as soon as you dequeue a node with no children.
  3. If you write it recursively, when one child is None, use the other child's depth instead of the minimum.

Production & complexity notes

When solving Minimum depth of binary tree on LeetCode (easy), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Level-order traversal to reinforce the underlying pattern.

What it practices

Breadth-first search reaching shallow nodes first, from Level-order traversal.

Not started

Sign in to save your learning progress.

Sign in to save