mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Update 0102.二叉树的层序遍历.md
This commit is contained in:
@ -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)
|
||||||
|
Reference in New Issue
Block a user