Skip to content
6
0
:
0
0

Hierarchy Level Collect

Hard

Trees

In "Hierarchy Level Collect", 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 = [11,12,13,null,14,15,null]
Output:[[11],[12,13],[14,15]]

For input root = [11,12,13,null,14,15,null], nodes are grouped by depth from top to bottom, yielding [[11],[12,13],[14,15]]. Therefore, return [[11],[12,13],[14,15]].

Example 2

Input:root = [21,22,23,24,null,null,25]
Output:[[21],[22,23],[24,25]]

For input root = [21,22,23,24,null,null,25], nodes are grouped by depth from top to bottom, yielding [[21],[22,23],[24,25]]. Therefore, return [[21],[22,23],[24,25]].

Constraints

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

Test Cases (2)

Test Case 1
Input:
[11,12,13,null,14,15,null]
Expected Output:
[[11],[12,13],[14,15]]
Test Case 2
Input:
[21,22,23,24,null,null,25]
Expected Output:
[[21],[22,23],[24,25]]