python traverse binary tree code example
Example 1: traversing a tree in python
"""Inorder Traversing"""
def inorder_traversing(self, root):
res = []
if root:
res = self.inorder_traversing(root.left)
res.append(root.data)
res = res + inorder_traversing(root.right)
return res
Example 2: traversing a tree in python
"""Post-order"""
def postorder_traversal(self, root):
res = []
if root:
res = self.predorder_traversal(root.left)
res = res + self.predorder_traversal(root.right)
res.append(root.data)
return res