DSA Tree Traversal
Language: Data Structures
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def postorder(node, result=None):
if result is None:
result = []
if node is None:
return result
postorder(node.left, result)
postorder(node.right, result)
result.append(node.value)
return result
# Tree: root 5; left child 3, right child 8.
# 3's children are 2 and 4; 8's children are 7 and 9.
root = Node(5,
Node(3, Node(2), Node(4)),
Node(8, Node(7), Node(9)))
print(postorder(root))
# [2, 4, 3, 7, 9, 8, 5]Output
Click Run to execute this code.