Given a binary tree, task is to do reverse inorder traversal using Morris Traversal.
In a binary tree with n nodes, there are n + 1 NULL pointers which waste memory. So, threaded binary trees makes use of these NULL pointers to save lots of Memory.
So, in Threaded Binary trees these NULL pointers will store some useful information.
1)Storing predecessor information in NULL left pointers only, called as left threaded binary trees.
2)Storing successor information in NULL right pointers only, called as right threaded binary trees.
3)Storing predecessor information in NULL left pointers and successor information in NULL right pointers, called as fully threaded binary trees or simply threaded binary trees.
Morris traversal can be used to do Inorder traversal, reverse Inorder traversal, Pre-order traversal with constant extra memory consumed O(1).
Reverse Morris Traversal : It is simply the reverse form of Morris Traversal.In reverse Morris traversal, first create links to the inorder successor of the current node and print the data using these links, and finally revert the changes to restore original tree, which will give a reverse inorder traversal.
1) Initialize Current as root. 2) While current is not NULL : 2.1) If current has no right child a) Visit the current node. b) Move to the left child of current. 2.2) Else, here we have 2 cases: a) Find the inorder successor of current node. Inorder successor is the left most node in the right subtree or right child itself. b) If the left child of the inorder successor is NULL: 1) Set current as the left child of its inorder successor. 2) Move current node to its right. c) Else, if the threaded link between the current node and it's inorder successor already exists : 1) Set left pointer of the inorder successor as NULL. 2) Visit Current node. 3) Move current to it's left child.
7 3 6 1 5 2 4
Time Complexity : O(n)
Auxiliary Space : O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Level order traversal of Binary Tree using Morris Traversal
- Inorder Non-threaded Binary Tree Traversal without Recursion or Stack
- Reverse zigzag Traversal of a Binary Tree
- Reverse Clockwise spiral traversal of a binary tree
- Reverse Anti Clockwise Spiral Traversal of a Binary Tree
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Threaded Binary Tree
- Threaded Binary Tree | Insertion
- Double Threaded Binary Search Tree
- Threaded Binary Search Tree | Deletion
- Morris traversal for Preorder
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Diagonal Traversal of Binary Tree
- Boundary Traversal of binary tree
- Density of Binary Tree in One Traversal
- Zig-Zag traversal of a Binary Tree using Recursion
- Middle To Up-Down Order traversal of a Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.