Trains the technique from
LeetCode 2196Create Binary Tree From DescriptionsThis 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 survey crew has re-traced a district heating network one connection at a time, in no particular order. Each note links[i] = [upstream_i, downstream_i, tap_i] says that valve downstream_i hangs directly off valve upstream_i: off that valve's left tap when tap_i is 1, and off its right tap when tap_i is 0.
Valve ids are distinct positive integers. Every valve has at most one valve on its left tap and at most one on its right tap, and the notes together describe a single network with exactly one valve at the top, which is the one no note ever lists as a downstream valve.
Rebuild the network and return it as a flat listing, laid out by this rule and no other. Put the top valve into a waiting line on its own. Then repeatedly take the valve at the front of the line: write its id at the end of the listing, then add to the back of the line what sits on its left tap and then what sits on its right tap, adding the marker null for a tap with nothing on it. When null reaches the front of the line, write null to the listing and add nothing. Stop when the line runs dry, then delete any null entries left sitting after the last valve id in the listing.
Example 1
Valve 41 is the only one no note feeds. It carries 27 on its left tap and 58 on its right, and both of those have nothing hanging off them, so the listing ends there.
Example 2
Valve 19 is at the top even though it appears in the second note. Valve 3 has an empty left tap, which shows up as a `null` inside the listing rather than at the end.
Example 3
Every connection here uses a left tap, so each valve leaves a `null` behind for the right tap it never fills, and only the run of `null` markers past the last id is deleted.
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_network(links: list[list[int]]) -> list:public List<Integer> rebuildNetwork(int[][] links)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.