Traverse binary search tree
How to traverse a binary search tree in python ? Assuming we have a TreeNode
class TreeNode:
def __init__(self, value):
self.val = value
self.left = None
self.right = None
Pre-order traverse
Refer from Technique - Tree Traversal#Preorder traversal
def traverse(node: TreeNode):
if node is None:
return
print(node.value)
traverse(node.left)
traverse(node.right)
In-order traverse
Refer from Technique - Tree Traversal#Inorder Traversal
def traverse(node: TreeNode):
if node is None:
return
traverse(node.left)
print(node.val)
traverse(node.right)
Post-order traverse
Refer from Technique - Tree Traversal#Postorder traversal
def traverse(node: TreeNode):
if node is None:
return
traverse(node.left)
traverse(node.right)
print(node.val)