Trains the technique from
LeetCode 1028Recover a Tree From Preorder TraversalThis 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 inspector walks a pipeline that branches downstream from a single intake. Every junction carries a flow-meter reading. A junction feeds at most two junctions below it, called its first and second outlet, and when a junction feeds only one junction that one is its first outlet.
Her walk goes into a single line log. She notes a junction's reading, then everything reachable through its first outlet, then everything reachable through its second outlet, following that same order at every junction she reaches. Each reading is written after one '-' for each step the junction lies below the intake, so the intake's reading opens the line with no dash and a junction three steps down carries three dashes. A reading is plain decimal digits, and dashes are the only other character in the line.
Rebuild the pipeline and return it as a band listing, one band at a time from the intake down, and left to right within a band. Slot 0 holds the intake. Each junction that appears in the listing takes the next two unclaimed slots for what it feeds, the first outlet then the second. A slot holding null means nothing is fed there, and such a slot claims no slots of its own. Slots that would hold null at the very end of the listing must be left off.
Exactly one pipeline matches a given line, so the listing is unique.
Example 1
The intake reads 5 and feeds 3 and 4. Junction 3 feeds 8 and 9, and junction 4 feeds only 2, which therefore sits on its first outlet.
Example 2
Four meters, two of them multi-digit beyond two places. The intake reads 10, its first outlet reads 200 with 3000 below it, and its second outlet reads 45.
Example 3
After the reading 4, which lies two steps below the intake, the reading 5 carries a single dash, so it sits one step below the intake alongside 2.
Example 4
A single reading with no dash: the pipeline is one junction and nothing is fed from it.
Example 5
Each reading is one dash deeper than the one before it, so the pipeline runs straight down through first outlets and the empty second outlets show up inside the listing.
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 recover_from_preorder(log: str) -> list[int | None]:public Integer[] recoverFromPreorder(String log)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.