Trains the technique from
LeetCode 257Binary Tree PathsThis 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.
A caving club has surveyed a dry limestone system. One chamber is the entrance, and every chamber has at most two onward passages leading deeper, one that the surveyors call the left passage and one they call the right. No passage ever rejoins a chamber already visited, so the system branches and never closes a loop. Each chamber is stencilled with its floor height in metres relative to the water table, which may be negative.
The survey reaches you as chambers, listing the system one depth band at a time, left to right. Slot 0 holds the entrance. Every chamber in the listing claims the next two unclaimed slots for its passages, the left one first and the right one second. A slot holding null means no chamber hangs there, and such a slot claims no slots of its own. Trailing null slots may be left off the end of the listing.
A route starts at the entrance, follows onward passages, and finishes at a chamber with no onward passage at all. Write a route as the stencilled heights of the chambers it passes through, entrance first, joined by forward slashes, for example 12/-4/0.
Return every route in the system. The routes may come back in any order.
Example 1
The entrance has both passages, and each leads straight to a chamber with nothing beyond it, so there are two routes of two chambers each.
Example 2
The entrance has only a left passage, and the chamber it reaches has only a left passage of its own, so the single route runs three chambers deep.
Example 3
The entrance is also a dead end, so the one route consists of it alone.
Example 4
Three chambers in this system have no onward passage, and each contributes the sequence of heights read from the entrance down to it.
The values you return may be in any order.
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 binary_tree_paths(chambers: list[int | None]) -> list[str]:public List<String> binaryTreePaths(Integer[] chambers)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.