Skip to content
6
0
:
0
0

Node Level Aggregation

Medium

Trees

In "Node Level Aggregation", the binary tree is provided in array form (level-order with null gaps).

Return node values grouped by depth from top to bottom and left to right.

Example 1

Input:root = [17,18,19,null,20,21,null]
Output:[[17],[18,19],[20,21]]

For input root = [17,18,19,null,20,21,null], nodes are grouped by depth from top to bottom, yielding [[17],[18,19],[20,21]]. Therefore, return [[17],[18,19],[20,21]].

Example 2

Input:root = [27,28,29,30,null,null,31]
Output:[[27],[28,29],[30,31]]

For input root = [27,28,29,30,null,null,31], nodes are grouped by depth from top to bottom, yielding [[27],[28,29],[30,31]]. Therefore, return [[27],[28,29],[30,31]].

Constraints

  • 0 <= number of nodes <= 2000
  • -1000 <= Node.val <= 1000
solution.js
Loading...

Test Cases (2)

Test Case 1
Input:
[17,18,19,null,20,21,null]
Expected Output:
[[17],[18,19],[20,21]]
Test Case 2
Input:
[27,28,29,30,null,null,31]
Expected Output:
[[27],[28,29],[30,31]]