mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
29 lines
728 B
Python
29 lines
728 B
Python
# recursive and iterative inorder traversal
|
|
|
|
def preorder_recursive(root: Optional[TreeNode]) -> list[int]:
|
|
|
|
if root == None:
|
|
return []
|
|
|
|
return [root.val] + preorder_recursive(root.left) + preorder_recursive(root.right)
|
|
|
|
|
|
def preorder_iterative(root: Optional[TreeNode]) -> list[int]:
|
|
|
|
result = []
|
|
stack = [root]
|
|
|
|
while stack:
|
|
|
|
current = stack.pop()
|
|
result.append(current.val)
|
|
|
|
if current.right:
|
|
stack.append(current.right)
|
|
|
|
if current.left:
|
|
stack.append(current.left)
|
|
|
|
return result
|
|
|