1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
func levelOrder(root *Node) [][]int {
if root == nil {
return nil
}
var res [][]int
dfs(root, 0, &res)
return res
}
func dfs(root *Node, level int, res *[][]int) {
if root == nil {
return
}
if len(*res) == level {
*res = append(*res, []int{})
}
(*res)[level] = append((*res)[level], root.Val)
for _, child := range root.Children {
dfs(child, level+1, res)
}
}
|