Trains the technique from
LeetCode 111Minimum Depth of Binary TreeThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
An approval workflow is drawn as a binary flowchart. Each box carries a signed number and has at most two follow-on boxes, one drawn to its left and one to its right. A box with no follow-on on either side is a terminal box.
The chart arrives as chart, a level-by-level listing. chart[0] is the entry box. After that, every box already listed contributes two entries in turn, its left follow-on then its right follow-on, with null where that follow-on is missing. A null contributes nothing further, and null entries at the very end of the listing are left off. An empty listing means there is no chart at all.
A route runs from the entry box down through follow-on boxes, and its length is the number of boxes on it, the entry box counted. Return the length of the shortest route that ends at a terminal box, or 0 when the listing is empty.
Example 1
Box 12 is a follow-on of the entry box and has no follow-on of its own, so the route entry box then 12 counts two boxes.
Example 2
The chart is a single line of three boxes, and the third of them is the first one on that line with no follow-on.
Example 3
The listing is empty, so there is no chart and the answer is 0.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def shortest_route_to_terminal(chart: list[int | None]) -> int:public int shortestRouteToTerminal(Integer[] chart)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.