## Question:

Given a binary tree root node, convert that tree to it’s mirror.

## Example:

Consider the image given below and its mirror.

Above image shows a tree and it’s mirror tree.

From the above image we can see that, except the root node, all the other nodes have changed from right to left or left to right.

## For example:

node b was left, it has been changed to right node in mirror tree

node c was right, it has been changed to left node in mirror tree

Similarly for

node d was left, it has been changed to right node in mirror tree

node e was right, it has been changed to left node in mirror tree

so this is how we can convert a binary tree to its Mirror

## Solution in C++

#include <iostream> #include <queue> #include <stack> using namespace std; // structure to hold binary tree node struct Node { int data; Node *left, *right; Node(int data) { this->data = data; this->left = this->right = nullptr; } }; void inorder(Node *node) { if(node == NULL) { return; } inorder(node->left); cout<< node->data << " "; inorder(node->right); } Node * convert_mirror_tree(Node *node) { if(node == NULL) { return NULL; } Node *temp = node->left; node->left = node->right; node->right = temp; convert_mirror_tree(node->left); convert_mirror_tree(node->right); return node; } int main() { Node* root = nullptr; /* Binary tree: 16 / \ / \ 10 25 / \ / \ / \ / \ 7 15 18 30 */ root = new Node(16); root->left = new Node(10); root->right = new Node(25); root->left->left = new Node(7); root->left->right = new Node(15); root->right->left = new Node(18); root->right->right = new Node(30); cout<<"Before convert inorder traversal = "<<endl; inorder(root); cout<<endl; Node *result = convert_mirror_tree(root); cout<<"After convert inorder traversal = "<<endl; inorder(root); cout<<endl; return 0; }

## Output:

Before convert inorder traversal = 7 10 15 16 18 25 30 After convert inorder traversal = 30 25 18 16 15 10 7