1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
func levelOrder(root *TreeNode) [][]int {
if root == nil {
return nil
}
var res [][]int
dfs(root, 0, &res)
return res
}
func dfs(root *TreeNode, level int, res *[][]int) {
if root == nil {
return
}
if level == len(*res) {
*res = append(*res, []int{})
}
(*res)[level] = append((*res)[level], root.Val)
if root.Left != nil {
dfs(root.Left, level+1, res)
}
if root.Right != nil {
dfs(root.Right, level+1, res)
}
}
|