mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
二叉树的层序遍历:使用切片模拟队列,易理解
This commit is contained in:
@ -271,6 +271,36 @@ func levelOrder(root *TreeNode) [][]int {
|
|||||||
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
102. 二叉树的层序遍历:使用切片模拟队列,易理解
|
||||||
|
*/
|
||||||
|
func levelOrder(root *TreeNode) (res [][]int) {
|
||||||
|
if root == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
curLevel := []*TreeNode{root} // 存放当前层节点
|
||||||
|
for len(curLevel) > 0 {
|
||||||
|
nextLevel := []*TreeNode{} // 准备通过当前层生成下一层
|
||||||
|
vals := []int{}
|
||||||
|
|
||||||
|
for _, node := range curLevel {
|
||||||
|
vals = append(vals, node.Val) // 收集当前层的值
|
||||||
|
// 收集下一层的节点
|
||||||
|
if node.Left != nil {
|
||||||
|
nextLevel = append(nextLevel, node.Left)
|
||||||
|
}
|
||||||
|
if node.Right != nil {
|
||||||
|
nextLevel = append(nextLevel, node.Right)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res = append(res, vals)
|
||||||
|
curLevel = nextLevel // 将下一层变成当前层
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
javascript代码:
|
javascript代码:
|
||||||
|
Reference in New Issue
Block a user