# Convert Binary Tree to its Mirror Tree

“**Convert Binary Tree to its Mirror Tree**” is a basic problem of tree data structure. Here, we are given a binary tree and our task is to write a program to convert given binary tree to its mirror tree.

This is a simple problem and can be solved using various traversal algorithm. Here, we are going to solve the given problem using postorder traversal and level order traversal. In both the traversal, we only need to swap the left and right child.

**Method 1: Using Postorder Traversal**

#### C++ Program to convert binary tree to its mirror tree is as follows:

```
/* C++ Program to Convert Binary Tree to its Mirror Tree */
#include<bits/stdc++.h>
using namespace std;
typedef struct Node
{
int data;
struct Node *left;
struct Node *right;
Node(int ele)
{
data = ele;
left = NULL;
right = NULL;
}
} Node;
/* Function to Convert Binary Tree to its Mirror Tree */
void convertMirror(Node *root)
{
/* Base Condition: Empty Tree */
if(root == NULL)
return;
convertMirror(root -> left);
convertMirror(root -> right);
swap(root -> left, root -> right);
}
/* Inorder Traversal */
void inorder(Node *root)
{
if(root == NULL)
return;
inorder(root -> left);
cout<<root -> data<<" ";
inorder(root -> right);
}
int main()
{
/* Creating a Binary trees and inserting some nodes in it */
Node *root = NULL;
root = new Node(1);
root -> left = new Node(2);
root -> right = new Node(3);
root -> left -> left = new Node(4);
root -> left -> right = new Node(5);
root -> right -> left = new Node(6);
root -> right -> right = new Node (7);
root -> left -> right -> left = new Node(8);
root -> right -> left -> right = new Node(9);
/* Printing Inorder Traversal before converting to Mirror Tree */
cout<<"Inorder Traversal of Original Tree:\n";
inorder(root);
/* Calling function to Convert Binary Tree to its Mirror Tree */
convertMirror(root);
/* Printing Inorder Traversal after converting to Mirror Tree */
cout<<"\nInorder Traversal of Mirror Tree:\n";
inorder(root);
}
```

**OUTPUT:**

Inorder Traversal of Original Tree: 4 2 8 5 1 6 9 3 7 Inorder Traversal of Mirror Tree: 7 3 9 6 1 5 8 2 4

**Method 2: Using Level Order Traversal**

#### C++ Program to convert binary tree to its mirror tree is as follows:

```
/* C++ Program to Convert Binary Tree to its Mirror Tree */
#include<bits/stdc++.h>
using namespace std;
typedef struct Node
{
int data;
struct Node *left;
struct Node *right;
Node(int ele)
{
data = ele;
left = NULL;
right = NULL;
}
} Node;
/* Function to Convert Binary Tree to its Mirror Tree */
void convertMirror(Node *root)
{
/* Base Condition: Empty Tree */
if(root == NULL)
return;
queue<Node *> qu;
qu.push(root);
while(!qu.empty())
{
Node *curr = qu.front();
qu.pop();
swap(curr -> left, curr -> right);
if(curr -> left)
qu.push(curr -> left);
if(curr -> right)
qu.push(curr -> right);
}
}
/* Inorder Traversal */
void inorder(Node *root)
{
if(root == NULL)
return;
inorder(root -> left);
cout<<root -> data<<" ";
inorder(root -> right);
}
int main()
{
/* Creating a Binary trees and inserting some nodes in it */
Node *root = NULL;
root = new Node(1);
root -> left = new Node(2);
root -> right = new Node(3);
root -> left -> left = new Node(4);
root -> left -> right = new Node(5);
root -> right -> left = new Node(6);
root -> right -> right = new Node (7);
root -> left -> right -> left = new Node(8);
root -> right -> left -> right = new Node(9);
/* Printing Inorder Traversal before converting to Mirror Tree */
cout<<"Inorder Traversal of Original Tree:\n";
inorder(root);
/* Calling function to Convert Binary Tree to its Mirror Tree */
convertMirror(root);
/* Printing Inorder Traversal after converting to Mirror Tree */
cout<<"\nInorder Traversal of Mirror Tree:\n";
inorder(root);
}
```

**OUTPUT:**

Inorder Traversal of Original Tree: 4 2 8 5 1 6 9 3 7 Inorder Traversal of Mirror Tree: 7 3 9 6 1 5 8 2 4

**Related Posts:**

**Check if Binary Tree is Symmetric or Not****Print All Root to Leaf Paths in a Binary Tree****Introduction to Tree Data Structure****Binary Tree Traversals****Print All Leaf Nodes of a Binary Tree****Count Number of Nodes in a Binary Tree****Print Alternate Levels of Binary Tree****Maximum Width of Binary Tree****Level Order Tree Traversal****Left View of Binary Tree****Right View of Binary Tree****Compute Height of Binary Tree****Inorder Tree Traversal Using Stack****Preorder Tree Trasversal Using Stack****Postorder Tree Traversal Using Stack****Vertical Order Tree Traversal****Top View of Binary Tree****Bottom View of Binary Tree****Delete Complete Binary Tree****Check if two trees are mirror Trees of Each Other**