Practice/Meta/Leetcode 429. N-ary Tree Level Order Traversal
CodingMust
Given the root of an n-ary tree (a tree where each node can have any number of children), return the values of its nodes organized by their depth level. The result should be a list of lists, where each inner list contains all node values at that particular level, ordered from left to right.
An n-ary tree is defined by a Node class with two properties:
val: an integer representing the node's valuechildren: a list of child nodes (which may be empty)Traverse the tree level by level (also known as breadth-first traversal) and group all values at the same depth together.
[0, 10000]-100 and 1001000 levelsExample 1:
`
Input: root = [1,null,3,2,4,null,5,6]
Tree structure:
1
/ |
3 2 4
/
5 6
Output: [[1], [3, 2, 4], [5, 6]] Explanation:
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]
Tree structure:
1
/ |
2 3 4
/ \ |
6 7 8
/ \ | |
11 12 13 14
/
9
/
10
Output: [[1], [2, 3, 4], [6, 7, 8], [11, 12, 13, 14], [9], [10]] `
Example 3:
Input: root = null Output: [] Explanation: Empty tree returns empty list