← DiffPush

Flatten Binary Tree to Linked List

DiffPush Tier Binary Trees · Hard O(n) · O(1)

The Conveyor Re-rack to a Single Lane

A distribution hub is switching from a tree of branch conveyors to one straight express lane. Every station must survive the re-rack, and the order along the new lane must be exactly the hub-first walk of the old tree — each station's left spur is dismantled and spliced into the lane ahead of its right spur, all rewiring done with the existing stations and zero spare parts.

Input: A binary tree as a level-order array (null marks a missing child).

Output: Return the station values along the flattened lane — the preorder sequence after rewiring every left pointer to null and every right pointer to the next station.

Constraints

Examples

Example 1

Input: {"tree":[1,2,5,3,4,null,6]}
Output: [1,2,3,4,5,6]
The preorder walk 1-2-3-4-5-6 becomes the single right-leaning lane.

Example 2

Input: {"tree":[1,null,2,3]}
Output: [1,2,3]
The left spur of node 2 splices in before nothing, so the lane keeps 1-2-3.

Solve this in your browser →

Also on LeetCode ↗