mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-07 15:01:58 +08:00
fix format error
This commit is contained in:
@ -1,17 +1,22 @@
|
||||
"""
|
||||
File: binary_tree_bfs.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
Created Time: 2022-12-20
|
||||
Author: a16su (lpluls001@gmail.com)
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
import typing
|
||||
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
||||
|
||||
def hierOrder(root):
|
||||
""" 层序遍历 """
|
||||
|
||||
|
||||
def hier_order(root: TreeNode):
|
||||
# 初始化队列,加入根结点
|
||||
queue = collections.deque()
|
||||
queue: typing.Deque[TreeNode] = collections.deque()
|
||||
queue.append(root)
|
||||
# 初始化一个列表,用于保存遍历序列
|
||||
result = []
|
||||
@ -33,13 +38,11 @@ def hierOrder(root):
|
||||
if __name__ == "__main__":
|
||||
# 初始化二叉树
|
||||
# 这里借助了一个从数组直接生成二叉树的函数
|
||||
root = list_to_tree(
|
||||
arr=[1, 2, 3, 4, 5, 6, 7, None, None, None, None, None, None, None, None]
|
||||
)
|
||||
root = list_to_tree(arr=[1, 2, 3, 4, 5, 6, 7, None, None, None, None, None, None, None, None])
|
||||
print("\n初始化二叉树\n")
|
||||
print_tree(root)
|
||||
|
||||
# 层序遍历
|
||||
result = hierOrder(root)
|
||||
result = hier_order(root)
|
||||
print("\n层序遍历的结点打印序列 = ", result)
|
||||
assert result == [1, 2, 3, 4, 5, 6, 7]
|
||||
assert result == [1, 2, 3, 4, 5, 6, 7]
|
||||
|
Reference in New Issue
Block a user