mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-01 18:55:23 +08:00
* feat(ko): add the complete Korean online edition * Use UTF-8 for Python CI output on Windows * Remove print-book promotion from the Korean site * fix(ko): preserve emphasis before Korean particles
36 lines
836 B
Go
36 lines
836 B
Go
// File: binary_tree_bfs.go
|
|
// Created Time: 2022-11-26
|
|
// Author: Reanon ([email protected])
|
|
|
|
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
|
|
}
|