Using Morris Traversal, we can traverse the tree without using stack and recursion. The algorithm for Preorder is almost similar to Morris traversal for Inorder.
1...If left child is null, print the current node data. Move to right child.
….Else, Make the right child of the inorder predecessor point to the current node. Two cases arise:
………a) The right child of the inorder predecessor already points to the current node. Set right child to NULL. Move to right child of current node.
………b) The right child is NULL. Set it to current node. Print current node’s data and move to left child of current node.
2...Iterate until current node is not NULL.
Following is the implementation of the above algorithm.
1 2 4 8 9 5 10 11 3 6 7 1 2 4 8 9 5 10 11 3 6 7
Morris traversal modifies the tree during the process. It establishes the right links while moving down the tree and resets the right links while moving up the tree. So the algorithm cannot be applied if write operations are not allowed.
This article is compiled by Aashish Barnwal and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Tree Traversals (Inorder, Preorder and Postorder)
- If you are given two traversal sequences, can you construct the binary tree?
- Level Order Tree Traversal
- Level order traversal in spiral form
- Inorder Tree Traversal without Recursion
- Inorder Tree Traversal without recursion and without stack!
- Construct Tree from given Inorder and Preorder traversals
- Construct Special Binary Tree from given Inorder traversal
- Construct a special tree from given preorder traversal
- Boundary Traversal of binary tree
- Construct Full Binary Tree from given preorder and postorder traversals
- Construct BST from given preorder traversal | Set 2
- Iterative Preorder Traversal
- Iterative Postorder Traversal | Set 1 (Using Two Stacks)
- Iterative Postorder Traversal | Set 2 (Using One Stack)