Problem Statement in English
You have to implement a preorder, inorder, and postorder traversal of a binary tree without using recursion or a stack. How do you do it?
Approach
There’s a subproblem that we need in order (hehe , pun intended) to be able to perfrom the traversal. We need to be able to find the predecessor of a node in a binary tree.
In the case of an inorder and preorder traversal, the predecessor of a node is the rightmost node in its left subtree. We will get to the case of the postorder traversal in just a bit.
Why is the predecessor of a node in an inorder and preorder traversal the rightmost node in its left subtree?
It’s because in an inorder traversal, we visit the left subtree first, then the node itself, and finally the right subtree. Therefore, the predecessor of a node is the last node we visit in its left subtree before visiting the node itself, which is the rightmost node.
In the case of a preorder traversal, we visit the node itself first, then the left subtree, and finally the right subtree. To be able to visit the right subtree after the left subtree, we still need to get back to the node, even though we visit the node itself before the left subtree. Therefore, the predecessor of a node is still the rightmost node in its left subtree.
Since we can’t use recursion or a stack, once we reach this predecessor there’s no way for us to get back to the node itself.
So we create a temporary link from the predecessor to the node itself. This way, when we reach the predecessor, we can follow this temporary link back to the node itself, and then later clean up that temporary link.
So in an inorder and preorder traversal, when we’re at a node, before we go to its left subtree, we find its predecessor.
If the predecessor doesn’t have a right child, we create a temporary link from the predecessor to the node itself and then go to the left subtree.
If the predecessor already has a right child (which means we’ve already visited the left subtree), we remove the temporary link and then visit the node itself (in the case of an inorder traversal) or visit the node itself before going to the left subtree (in the case of a preorder traversal).
How about the postorder traversal? It’s a little tricky to implement a postorder traversal using the exact same approach, and so to get around this we can do a reverse preorder traversal (Node -> Right -> Left) and then reverse the result at the end. This is much easier.
However, it requires minor tweaks to the predecessor finding function. In this case, the predecessor of a node is the leftmost node in its right subtree. The rest of the logic is the same as before.
Find Predecessor Function
In the case of an inorder and preorder traversal, the predecessor of a node is the rightmost node in its left subtree. In the case of a postorder traversal (at least for the sake of our implementation), the predecessor of a node is the leftmost node in its right subtree.
Solution in Python
- Inorder Traversal
class Solution:
def inorderTraversal(self, root: TreeNode | None) -> list[int]:
def findIP(node):
temp = node.left
while temp.right and temp.right != node:
temp = temp.right
return temp
curr = root
res = []
while curr:
if curr.left:
ip = findIP(curr)
if ip.right is None:
ip.right = curr
curr = curr.left
else:
ip.right = None
res.append(curr.val)
curr = curr.right
else:
res.append(curr.val)
curr = curr.right
return res
- Preorder Traversal
class Solution:
def preorderTraversal(self, root: TreeNode | None) -> list[int]:
def findPredecessor(node):
temp = node.left
while temp.right and temp.right != node:
temp = temp.right
return temp
curr = root
res = []
while curr:
if curr.left:
predecessor = findPredecessor(curr)
if predecessor.right is None:
predecessor.right = curr
res.append(curr.val)
curr = curr.left
else:
predecessor.right = None
curr = curr.right
else:
res.append(curr.val)
curr = curr.right
return res
- Postorder Traversal
class Solution:
def postorderTraversal(self, root: TreeNode | None) -> list[int]:
def findPredecessor(node):
temp = node.right
while temp.left and temp.left != node:
temp = temp.left
return temp
curr = root
res = []
while curr:
if curr.right:
predecessor = findPredecessor(curr)
if predecessor.left is None:
res.append(curr.val)
predecessor.left = curr
curr = curr.right
else:
predecessor.left = None
curr = curr.left
else:
res.append(curr.val)
curr = curr.left
res.reverse()
return res
Complexity
Time: $O(n)$
Since we are visiting each node at most twice, the time complexity is linear in the number of nodes in the tree.Space: $O(1)$
Since we are not using any extra space for recursion or a stack, the space complexity is constant.
And we are done.