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 inorder(node, result=None):
if result is None:
result = []
if node is None:
return result
inorder(node.left, result)
result.append(node.value)
inorder(node.right, result)
return result
# Tree: root 8; left child 3, right child 10.
# 3's children are 1 and 6; 6's children are 4 and 7.
# 10's right child is 14, and 14's left child is 13.
root = Node(8,
Node(3, Node(1), Node(6, Node(4), Node(7))),
Node(10, None, Node(14, Node(13))))
print(inorder(root))
# [1, 3, 4, 6, 7, 8, 10, 13, 14]Output
Click Run to execute this code.