mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 12:16:14 -04:00
32 lines
605 B
Python
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
|
|
|