LeetCode 429. N 叉树的层序遍历
题目描述
思路分析
层序遍历
参考代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
func levelOrder(root *Node) [][]int {
res := make([][]int, 0)
if root == nil {
return res
}
queue := []*Node{root}
for len(queue) > 0 {
level, size := make([]int, 0), len(queue)
for i := 0; i < size; i++ {
node := queue[0]
level = append(level, node.Val)
queue = queue[1:]
queue = append(queue, node.Children...)
}
res = append(res, level)
}
return res
}
1
write your code here
CC BY-NC-SA 4.0
许可协议,转载请注明出处!
本博客所有文章除特别声明外,均采用