This commit is contained in:
bt3gl 2023-07-30 21:40:09 -07:00
parent 1d44d182e2
commit a85ed914d3
320 changed files with 0 additions and 0 deletions

View file

@ -1,32 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
# 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