mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge pull request #147 from QuinnDK/添加0102二叉树的层序遍历Go版本
添加0102二叉树的层序遍历Go版本
This commit is contained in:
@ -657,8 +657,36 @@ Python:
|
||||
|
||||
|
||||
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)
|
||||
|
Reference in New Issue
Block a user