/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funclevelOrder(root *TreeNode)[][]int{var order func(root *TreeNode, depth int)var res [][]intvar tmp []int
order =func(root *TreeNode, depth int){if root ==nil{return}if depth ==len(res){
res =append(res, tmp)}
res[depth]=append(res[depth], root.Val)order(root.Left,depth+1)order(root.Right,depth+1)}order(root,0)return res
}
遍历
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funclevelOrder(root *TreeNode)[][]int{var res [][]intvar tmp []intif root ==nil{return res
}
queue := list.New()
queue.PushBack(root)for queue.Len()>0{
length := queue.Len()for i :=0; i < length ; i++{
node := queue.Remove(queue.Front()).(*TreeNode)
tmp =append(tmp , node.Val)if(node.Left !=nil){
queue.PushBack(node.Left)}if(node.Right !=nil){
queue.PushBack(node.Right)}}
res =append(res , tmp)
tmp =[]int{}}return res
}