Given a binary tree, how to convert it to it’s mirror image?
For example consider the following binary tree and it’s mirror image.
For example consider the following binary tree and it’s mirror image.
Here simply, the left and right sub trees of each node are inverted. So we have to simply invert the given binary tree. It is a simple implementation problem. Using recursion we can solve it by following the below steps
- If root is NULL, simply return
- Invert the left sub tree
- Invert the right sub tree
- Swap left and right sub trees.
Here is the C++ code which implements the above algorithm. It runs in O(n) time.