Tree Traversal
2 min readMay 30, 2019
Well I have 4 different types of tree traversals that come to my mind when i think about it :
- InOrder
- PreOrder
- PostOrder
- Vertical Order Traversal
- Level Order Traversal [Covered in Some other Lesson]
Inorder Traversal
Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)
My Java Based Implementation for it
PostOrder Traversal
Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.
My Java Based Implementation for it
PreOrder Traversal
Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
My Java Based Implementation for it
Vertical Order Traversal
So the trick in vertical order is to understand the notion of horizontal distance . All items with same horizontal distance should be printed in a vertical fashion