Update 0102.二叉树的层序遍历.md

This commit is contained in:
QuinnDK
2021-05-16 16:47:45 +08:00
committed by GitHub
parent 8c3d8aab25
commit e3367b5332

View File

@ -657,8 +657,36 @@ Python
Go Go
```Go
func levelOrder(root *TreeNode) [][]int {
result:=make([][]int,0)
if root==nil{
return result
}
queue:=make([]*TreeNode,0)
queue=append(queue,root)
for len(queue)>0{
list:=make([]int,0)
l:=len(queue)
for i:=0;i<l;i++{
level:=queue[0]
queue=queue[1:]
list=append(list,level.Val)
if level.Left!=nil{
queue=append(queue,level.Left)
}
if level.Right!=nil{
queue=append(queue,level.Right)
}
}
result=append(result,list)
}
return result
}
```
----------------------- -----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)