mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-01 03:24:24 +08:00
* docs: add Japanese documents (`ja/docs`) * docs: add Japanese documents (`ja/codes`) * docs: add Japanese documents * Remove pythontutor blocks in ja/ * Add an empty at the end of each markdown file. * Add the missing figures (use the English version temporarily). * Add index.md for Japanese version. * Add index.html for Japanese version. * Add missing index.assets * Fix backtracking_algorithm.md for Japanese version. * Add avatar_eltociear.jpg. Fix image links on the Japanese landing page. * Add the Japanese banner. --------- Co-authored-by: krahets <krahets@163.com>
42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
"""
|
|
File: binary_tree_bfs.py
|
|
Created Time: 2022-12-20
|
|
Author: a16su (lpluls001@gmail.com)
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.append(str(Path(__file__).parent.parent))
|
|
from modules import TreeNode, list_to_tree, print_tree
|
|
from collections import deque
|
|
|
|
|
|
def level_order(root: TreeNode | None) -> list[int]:
|
|
"""レベル順走査"""
|
|
# キューを初期化し、ルートノードを追加
|
|
queue: deque[TreeNode] = deque()
|
|
queue.append(root)
|
|
# 走査シーケンスを格納するリストを初期化
|
|
res = []
|
|
while queue:
|
|
node: TreeNode = queue.popleft() # キューからデキュー
|
|
res.append(node.val) # ノードの値を保存
|
|
if node.left is not None:
|
|
queue.append(node.left) # 左の子ノードをエンキュー
|
|
if node.right is not None:
|
|
queue.append(node.right) # 右の子ノードをエンキュー
|
|
return res
|
|
|
|
|
|
"""ドライバコード"""
|
|
if __name__ == "__main__":
|
|
# 二分木を初期化
|
|
# 特定の関数を使用して配列を二分木に変換
|
|
root: TreeNode = list_to_tree(arr=[1, 2, 3, 4, 5, 6, 7])
|
|
print("\n二分木を初期化\n")
|
|
print_tree(root)
|
|
|
|
# レベル順走査
|
|
res: list[int] = level_order(root)
|
|
print("\nレベル順走査のノードシーケンスを出力 = ", res) |