#429
N-ary Tree Level Order Traversal
pupil · 540 · lc medium +28 · verified · 71.5% accepted · 3,744 likes · top 80%
Description
Given the root of an N-ary tree (where nodes can have any number of children), produce a level-order traversal. Group the values of all nodes at the same depth into one inner list.
In the serialized input, children of each node appear consecutively, and null marks the boundary between sibling groups.
Example 1:
Input: root = [1,null,3,2,4,null,5,6]
Output: [[1],[3,2,4],[5,6]]
Example 2:
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
Code
1
2
3
4
5
6
7
8
9
10
11