mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-10 16:00:36 +08:00
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
This commit is contained in:
@@ -15,7 +15,7 @@ def pre_order(root: TreeNode | None):
|
||||
"""Preorder traversal"""
|
||||
if root is None:
|
||||
return
|
||||
# Visit priority: root node -> left subtree -> right subtree
|
||||
# Visit order: root -> left subtree -> right subtree
|
||||
res.append(root.val)
|
||||
pre_order(root=root.left)
|
||||
pre_order(root=root.right)
|
||||
@@ -25,7 +25,7 @@ def in_order(root: TreeNode | None):
|
||||
"""Inorder traversal"""
|
||||
if root is None:
|
||||
return
|
||||
# Visit priority: left subtree -> root node -> right subtree
|
||||
# Visit order: left subtree -> root -> right subtree
|
||||
in_order(root=root.left)
|
||||
res.append(root.val)
|
||||
in_order(root=root.right)
|
||||
@@ -35,31 +35,31 @@ def post_order(root: TreeNode | None):
|
||||
"""Postorder traversal"""
|
||||
if root is None:
|
||||
return
|
||||
# Visit priority: left subtree -> right subtree -> root node
|
||||
# Visit order: left subtree -> right subtree -> root
|
||||
post_order(root=root.left)
|
||||
post_order(root=root.right)
|
||||
res.append(root.val)
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
"""Driver code"""
|
||||
if __name__ == "__main__":
|
||||
# Initialize binary tree
|
||||
# Here we use a function to generate a binary tree directly from an array
|
||||
# Initialize the binary tree
|
||||
# Use a helper function that builds a binary tree directly from an array
|
||||
root = list_to_tree(arr=[1, 2, 3, 4, 5, 6, 7])
|
||||
print("\nInitialize binary tree\n")
|
||||
print("\nInitialize the binary tree\n")
|
||||
print_tree(root)
|
||||
|
||||
# Preorder traversal
|
||||
res = []
|
||||
pre_order(root)
|
||||
print("\nPreorder traversal node print sequence = ", res)
|
||||
print("\nNode sequence from preorder traversal = ", res)
|
||||
|
||||
# Inorder traversal
|
||||
res.clear()
|
||||
in_order(root)
|
||||
print("\nInorder traversal node print sequence = ", res)
|
||||
print("\nNode sequence from inorder traversal = ", res)
|
||||
|
||||
# Postorder traversal
|
||||
res.clear()
|
||||
post_order(root)
|
||||
print("\nPostorder traversal node print sequence = ", res)
|
||||
print("\nNode sequence from postorder traversal = ", res)
|
||||
|
||||
Reference in New Issue
Block a user