Find the Shortest Hop Count to Any Leaf Delivery Station

Last updated: November 29, 2026

Quick Overview

A regional delivery network is modeled as a binary tree whose root is the sort center and whose leaves are last-mile stations. Return the number of nodes on the shortest path from the root down to any leaf.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


A regional delivery network is modeled as a binary tree whose root is the sort center and whose leaves are last-mile stations. Return the number of nodes on the shortest path from the root down to any leaf.

Amazon phone screens often open with a short tree traversal to see whether you reach for the right search order. Here the interviewer is watching for the classic trap where a node with one child is wrongly treated as a leaf, and whether you know BFS can stop early.

What the Interviewer Expects
  • Define a leaf correctly as a node with no children
  • Prefer BFS so the search stops at the first leaf found
  • Avoid the min(left, right) bug when one child is missing
  • Return 0 for an empty tree
  • Explain why BFS beats DFS on lopsided trees
Key Topics to Cover
binary trees
BFS
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
  • How would you return the actual path to the nearest station, not just its depth?
  • What if each edge had a travel time and you wanted the cheapest leaf?
  • How does the answer change if the network is a general tree with many children?
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 of a binary tree, possibly empty. Output: an integer depth counted in nodes, so a single root is depth 1 and an empty tree is 0. A leaf has no left and no right child; a node with one chil...

High-Level Design

Level-order traversal visits nodes in increasing depth, so the first leaf dequeued is the shallowest. That means we can return immediately without exploring the rest of a possibly huge subtree. A recu...


Submit Your Answer
Markdown supported