master-algorithms-py/trees/bt_inorder.py
2023-08-08 14:27:07 -07:00

32 lines
605 B
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
def inorder(root) -> list:
if root is None:
return []
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