mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 03:30:30 +08:00
build
This commit is contained in:
@@ -69,13 +69,13 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree_bfs.py"
|
||||
def level_order(root: Optional[TreeNode]) -> List[int]:
|
||||
def level_order(root: TreeNode | None) -> list[int]:
|
||||
""" 层序遍历 """
|
||||
# 初始化队列,加入根结点
|
||||
queue: Deque[TreeNode] = collections.deque()
|
||||
queue: deque[TreeNode] = deque()
|
||||
queue.append(root)
|
||||
# 初始化一个列表,用于保存遍历序列
|
||||
res: List[int] = []
|
||||
res: list[int] = []
|
||||
while queue:
|
||||
node: TreeNode = queue.popleft() # 队列出队
|
||||
res.append(node.val) # 保存结点值
|
||||
@@ -337,7 +337,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree_dfs.py"
|
||||
def pre_order(root: Optional[TreeNode]) -> None:
|
||||
def pre_order(root: TreeNode | None) -> None:
|
||||
""" 前序遍历 """
|
||||
if root is None:
|
||||
return
|
||||
@@ -346,7 +346,7 @@ comments: true
|
||||
pre_order(root=root.left)
|
||||
pre_order(root=root.right)
|
||||
|
||||
def in_order(root: Optional[TreeNode]) -> None:
|
||||
def in_order(root: TreeNode | None) -> None:
|
||||
""" 中序遍历 """
|
||||
if root is None:
|
||||
return
|
||||
@@ -355,7 +355,7 @@ comments: true
|
||||
res.append(root.val)
|
||||
in_order(root=root.right)
|
||||
|
||||
def post_order(root: Optional[TreeNode]) -> None:
|
||||
def post_order(root: TreeNode | None) -> None:
|
||||
""" 后序遍历 """
|
||||
if root is None:
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user