Trains the technique from
LeetCode 145Binary Tree 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 workshop builds a machine from nested sub-assemblies. Each unit is stamped with a signed part code, and each unit is fitted out of at most two sub-assemblies, one the fitters call the left one and one they call the right one. No sub-assembly ever contains a unit that already contains it, so the parts branch and never close a loop.
The machine arrives as units, a level-order listing. Its first entry is the part code of the outermost unit. After that, the listing gives the left then right sub-assembly of each unit already listed, in the order those units appear, writing null where a unit has no sub-assembly on that side. Slots below a null are never written down. An empty listing means there is nothing to build.
A unit can only be fitted once both of its sub-assemblies are already fitted, and the left one is always fitted before the right one. Return the part codes in the order the fitters log them.
Example 1
Unit 4 holds sub-assemblies 2 and 6, and unit 7 holds 4 on the left and 9 on the right. Logging 2, then 6, then 4, then 9, then 7 has every unit's sub-assemblies already logged before the unit itself, with each left one ahead of its right one.
Example 2
Unit 8 has no left sub-assembly, so its right one, unit 6, is next in the listing. Unit 6 holds only a left sub-assembly, unit 4. The log is 4, then 6, then 8.
Example 3
Unit 9 holds 4 and 12, and unit 30 has no left sub-assembly but holds 41 on the right. Reading the log back, every unit appears after both of the units it holds.
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 assembly_order(units: list[int | None]) -> list[int]:public List<Integer> assemblyOrder(Integer[] units)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.