VOOZH about

URL: https://www.geeksforgeeks.org/dsa/write-an-efficient-c-function-to-convert-a-tree-into-its-mirror-tree/

⇱ Invert Binary Tree - Change to Mirror Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Invert Binary Tree - Change to Mirror Tree

Last Updated : 6 Oct, 2025

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.

Example:

Input:

👁 420046772

Output:

👁 420046773

Explanation: In the inverted tree, every non-leaf node has its left and right child interchanged.

👁 420046692


Input:

👁 420046775

Output:

👁 420046776

Explanation: In the inverted tree, every non-leaf node has its left and right child interchanged.

👁 420046774

[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.


Output
1 
3 2 
N N 5 4 
Comment