49. Binary Tree Level Order Traversal

Medium · Binary Tree

Given a binary tree represented as a level-order array (where null represents a missing node), return the values of all nodes grouped by their depth level, from top to bottom.

The input is a level-order array where the first element is the root, and for each node at index i, its left child is at index 2i+1 and right child is at index 2i+2 (null values indicate missing nodes). Your function should return a 2D array where each inner array contains all node values at that depth level, ordered from the root level downwards.

Examples

Example 1
Input: [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]
Explanation: Three levels of the tree, top-down
Example 2
Input: [1]
Output: [[1]]
Explanation: Single-node tree → one level with the root

Constraints