Find the Shallowest Leaf of a Decision Tree Using BFS

Last updated: November 29, 2026

Quick Overview

Given the root of a binary tree, return the number of nodes on the shortest path from the root down to any leaf, where a leaf has no children.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Easy

0

0

0 solved


Given the root of a binary tree, return the number of nodes on the shortest path from the root down to any leaf, where a leaf has no children.

A technical screen question where the interviewer uses the minimum depth of a tree to see if you pick BFS for shortest-path style questions. Imagine checking how quickly a decision tree used in a ranking feature can reach a terminal decision.

What the Interviewer Expects
  • Choose BFS so the first leaf found is the answer
  • Define a leaf correctly: both children missing
  • Avoid the bug of treating a missing child as depth 0
  • Handle empty tree
  • Compare with a DFS solution and when each is better
Key Topics to Cover
BFS
binary tree
minimum depth
early termination
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What is the worst case for DFS compared to BFS here?
  • Return the path to the shallowest leaf.
  • Find all leaves at the minimum depth.
  • How would you do this for an N-ary tree?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: root, possibly None.
  • Output: count of nodes on the shortest root-to-leaf path; 0 for an empty tree.
  • A node with one child is not a leaf....
High-Level Design

BFS explores nodes in order of depth, so the first leaf dequeued is at the minimum depth and we can stop immediately. That matters for a lopsided tree with a leaf near the top and a huge subtree elsew...


Submit Your Answer
Markdown supported