← DiffPush

Level Order Traversal of Binary Tree

Standard Bar Binary Trees · Traversals O(n) · O(n)

The Grandstand Wave Call

A stadium section is arranged as a binary tree of seating clusters, and the event host starts the wave from the head cluster. Each heartbeat, everyone exactly one ring deeper from the head stands up, moving left to right within their ring. The broadcast team wants the standing order grouped by heartbeat, so the camera crew knows which ring to film during each beat.

Input: The root of a binary tree given as a level-order array where null marks a missing child.

Output: Return a list of lists: the i-th inner list holds all node values on tree level i, left to right.

Constraints

Examples

Example 1

Input: {"tree":[1,2,3,4,5,null,6]}
Output: [[1],[2,3],[4,5,6]]
Three levels: the root alone, then 2 and 3, then leaves 4, 5, 6 left to right.

Example 2

Input: {"tree":[7]}
Output: [[7]]
One node means one level containing only that node.

Solve this in your browser →

Also on LeetCode ↗