/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
var res [][]int
func levelOrder(root *TreeNode) [][]int {
if root == nil{
return nil
}
res = make([][]int, 0)
dfs(root, 0)
return res
}
func dfs(root *TreeNode, level int){
if root == nil{
return
}
if level == len(res){
res = append(res, []int{})
}
res[level] = append(res[level], root.Val)
dfs(root.Left, level+1)
dfs(root.Right,level+1)
}
作者:zhao-gao-long
链接:https://leetcode-cn.com/problems/binary-tree-level-order-traversal/solution/ren-yong-xun-huan-shen-yong-di-gui-by-zhao-gao-lon/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。