mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Update bt_inorder.py
This commit is contained in:
parent
a42c13186c
commit
15d407b4ef
@ -2,6 +2,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# author: bt3gl
|
# author: bt3gl
|
||||||
|
|
||||||
|
|
||||||
def inorder(root) -> list:
|
def inorder(root) -> list:
|
||||||
|
|
||||||
if root is None:
|
if root is None:
|
||||||
@ -9,3 +10,22 @@ def inorder(root) -> list:
|
|||||||
|
|
||||||
return inorder(root.left) + [root.val] + inorder(root.right)
|
return inorder(root.left) + [root.val] + inorder(root.right)
|
||||||
|
|
||||||
|
|
||||||
|
def inorder_iterative(root) -> list:
|
||||||
|
|
||||||
|
result = []
|
||||||
|
stack = []
|
||||||
|
node = root
|
||||||
|
|
||||||
|
while stack or node:
|
||||||
|
|
||||||
|
if node:
|
||||||
|
stack.append(node)
|
||||||
|
node = node.left
|
||||||
|
else:
|
||||||
|
node = stack.pop()
|
||||||
|
result.append(node.val)
|
||||||
|
node = node.right
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user