Can we perform inorder traversal on a binary tree without using recursion or a stack, while also keeping the space complexity constant?
This is where Morris traversal comes in.
We have seen inorder traversal of binary tree here - Binary Tree Traversal - Inorder. The iterative version involves stack. Now lets see Morris Traversal.
The idea of Morris Traversal is based on Threaded Binary Tree. This approach achieves inorder traversal by establishing temporary links within the tree itself. The data is then printed by following these links, and finally, the tree structure is restored to its original state.
Algorithm
- At each step we will check if current node has:
- No left child:
- Update traversal: Add current node to traversal
- Move Right: The current pointer is advanced to its right child.
- has left child: then establish a temp link:
- Find Rightmost in Left Subtree: It traverses the current node’s left subtree and locates the rightmost node.
- Link Establishment: The rightmost node’s right child pointer is set to point back to the current node, essentially creating a temporary link.
- Move Left: The current pointer is then moved to this left child, continuing the traversal process.
- No left child:
Although the tree is modified through the traversal, it is reverted back to its original shape after the completion.
Dry Run
To summarize, we create threaded pointers (predecssors to root) when the left child exists, and then traverse left child. Once left children are processed, we process current child.
Code
Java
|
|
Time Complexity
- Time Complexity : O(n) - We traverse through all nodes, and each vertex is processed once.
- Space Complexity: O(1)