Given the root of binary tree, Convert it into its mirror. A mirror of a binary tree is another tree in which the left and right children of every non-leaf node are interchanged.
[Approach 1] Recursive Approach - O(n) Time and O(n) Space
The idea is to use recursion to traverse the tree in Post Order (left, right, root) and while traversing each node, swap the left and right subtrees.
Output
1
3 2
N N 5 4
[Approach 2] Iterative Approach - O(n) Time and O(n) Space
The idea is to perform level order traversal (using a queue). For every node, swap its left and right children. This way, after processing all nodes, the tree becomes its mirror.