Trains the technique from
LeetCode 889Construct Binary Tree from Preorder and Postorder 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.
A parcel line is built as a binary tree of inspection stations. Every station has a distinct label and at most two downstream stations, one on its left branch and one on its right.
A single parcel is pushed through the whole rig. Two logs come out of that run:
entry_log records a station's label the moment the parcel arrives at it, which happens before the parcel goes down that station's left branch and then its right branch;exit_log records a station's label once both of its branches have been fully worked, so a station is written only after every station below it.Rebuild the rig. When the two logs do not pin down which side a lone downstream station sits on, put it on the left: for every station that has at least one downstream station, the label written immediately after it in entry_log is its left branch station.
Return the rebuilt rig as a flat level-order encoding: entry 0 is the root's label, and after that, taking the present stations in level-order, each one contributes the next two entries as its left slot and then its right slot, with null for an absent station. Drop trailing null entries.
Example 1
Station 3 is the root, station 5 sits on its left branch with station 1 below 5, and station 2 sits on the root's right branch. Running a parcel through that rig writes 3, 5, 1, 2 on arrival and 1, 5, 2, 3 on completion, which is exactly the pair given.
Example 2
Station 4 is the root and station 1 is its only downstream station. The logs do not say which branch it hangs from, and the stated rule puts it on the left.
Example 3
Root 2 has station 6 on its left and station 3 on its right; station 6 has 5 and 1 below it, and station 3 has 4 on its left. Both logs read back off that rig unchanged.
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 rebuild_station_tree(entry_log: list[int], exit_log: list[int]) -> list:public Integer[] rebuildStationTree(int[] entryLog, int[] exitLog)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.