429. N-ary Tree Level Order Traversal
Question
Given an n-ary tree, return the level order traversal of its nodes’ values.
Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
Solution
BFS搜索,层序遍历。
用level记录上一层的个数。
当队列不为空时,每次挤出level个节点,并将其子节点加入队列。
将当前层次的节点值加入列表。
最后更新level的数量。
Code
1 | /* |
429. N-ary Tree Level Order Traversal
https://xuanhe95.github.io/2022/09/04/429-N-ary-Tree-Level-Order-Traversal/