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
November 29, 20260
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample 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...