Files
hello-algo/en/codes/go/chapter_tree/binary_tree_bfs.go
Yudong Jin 14c314c08c Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
2026-10-31 03:35:51 +08:00

36 lines
772 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"
)
/* Level-order traversal */
func levelOrder(root *TreeNode) []any {
// Initialize a queue and enqueue the root
queue := list.New()
queue.PushBack(root)
// Initialize a slice to store the traversal sequence
nums := make([]any, 0)
for queue.Len() > 0 {
// Dequeue from the queue
node := queue.Remove(queue.Front()).(*TreeNode)
// Store the node value
nums = append(nums, node.Val)
if node.Left != nil {
// Enqueue the left child
queue.PushBack(node.Left)
}
if node.Right != nil {
// Enqueue the right child
queue.PushBack(node.Right)
}
}
return nums
}