mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-11 08:20:38 +08:00
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
36 lines
851 B
Go
36 lines
851 B
Go
// File: binary_tree_bfs.go
|
|
// Created Time: 2022-11-26
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
package chapter_tree
|
|
|
|
import (
|
|
"container/list"
|
|
|
|
. "github.com/krahets/hello-algo/pkg"
|
|
)
|
|
|
|
/* レベル順走査 */
|
|
func levelOrder(root *TreeNode) []any {
|
|
// キューを初期化し、根ノードを追加する
|
|
queue := list.New()
|
|
queue.PushBack(root)
|
|
// 走査順序を保存するスライスを初期化する
|
|
nums := make([]any, 0)
|
|
for queue.Len() > 0 {
|
|
// キューからデキューする
|
|
node := queue.Remove(queue.Front()).(*TreeNode)
|
|
// ノードの値を保存する
|
|
nums = append(nums, node.Val)
|
|
if node.Left != nil {
|
|
// 左の子ノードをエンキューする
|
|
queue.PushBack(node.Left)
|
|
}
|
|
if node.Right != nil {
|
|
// 右の子ノードをエンキューする
|
|
queue.PushBack(node.Right)
|
|
}
|
|
}
|
|
return nums
|
|
}
|