Update bt_inorder.py

This commit is contained in:
bt3gl 2023-08-08 14:27:07 -07:00 committed by GitHub
parent a42c13186c
commit 15d407b4ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
# author: bt3gl
def inorder(root) -> list:
if root is None:
@ -9,3 +10,22 @@ def inorder(root) -> list:
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