Medium Trees

Level-order Traversal

Receives an array of integers and interprets it as a binary tree laid out level-order — index i's children live at 2i+1 and 2i+2, the same indexing scheme used by binary heaps. It then walks the tree breadth-first with a queue: starting from the root, each pass drains every node currently in the queue into one result row while enqueuing their children for the next pass. Returns an array of arrays, one per depth level (shallowest first), each holding that level's node values left to right.

Visualization

Input

Algorithm code

Custom input

Saved inputs

References