mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Update bt_preorder.py
This commit is contained in:
parent
5eba7a1a80
commit
a42c13186c
@ -9,3 +9,19 @@ def preorder(root) -> list:
|
|||||||
|
|
||||||
return [root.val] + preorder(root.left) + preorder(root.right)
|
return [root.val] + preorder(root.left) + preorder(root.right)
|
||||||
|
|
||||||
|
|
||||||
|
def preorder_iterative(root) -> list:
|
||||||
|
|
||||||
|
result = []
|
||||||
|
stack = [root]
|
||||||
|
|
||||||
|
while stack:
|
||||||
|
|
||||||
|
node = stack.pop()
|
||||||
|
|
||||||
|
if node:
|
||||||
|
result.append(node.val)
|
||||||
|
stack.append(node.right) # not the order
|
||||||
|
stack.append(node.left)
|
||||||
|
|
||||||
|
return result
|
||||||
|
Loading…
x
Reference in New Issue
Block a user