AlgoFrames

Build from a level-order array

Warm-up · Trace the algorithm · about 5 min

The array [8, 3, 10, 1, 6, null, 14] lists a binary tree level by level, left to right; null marks a missing child. The tree below has the right shape, but its values were placed depth-first. Put each value where the level-order array says it goes.

Example 1

Input:

{"levelOrder":[8,3,10,1,6,null,14]}

Output:

[8,3,10,1,6,null,14]

The values sit in the right shape but in the wrong slots.

Constraints

  • The trace uses the array shown

Learn the idea first: Binary trees and how they are stored, a visual lesson.

More Binary trees and how they are stored problems