← DiffPush

Binary Tree Representation

Baseline Binary Trees · Traversals O(n) · O(n)

The Storage-Bin Checkpoint Map

A fulfillment center keeps seven emergency supply bins laid out in a strict hierarchy: one master checkpoint, two wing supervisors beneath it, and four aisle wardens below them. The safety board receives the bin labels as a flat list in top-to-bottom, left-to-right order and must rebuild the physical hierarchy from it. To confirm the rebuild is wired correctly, the inspector walks the structure root-first and records labels as they are visited.

Input: An array of 7 integers given in level order — index 0 is the root, then its two children, then the four grandchildren.

Output: Return the preorder traversal (root, left subtree, right subtree) of the constructed tree as an array.

Constraints

Examples

Example 1

Input: {"levelOrder":[1,2,3,4,5,6,7]}
Output: [1,2,4,5,3,6,7]
Root 1, left subtree {2,4,5} visited root-first, then right subtree {3,6,7}.

Example 2

Input: {"levelOrder":[10,20,30,40,50,60,70]}
Output: [10,20,40,50,30,60,70]
Same shape as tc-1 — preorder is root, then the entire left wing, then the right wing.

Solve this in your browser →

Also on LeetCode ↗