# Create a mirror tree from the given binary tree

Given a binary tree, the task is to create a new binary tree which is a mirror image of the given binary tree.

Examples:

```Input:
5
/ \
3   6
/ \
2   4
Output:
Inorder of original tree: 2 3 4 5 6
Inorder of mirror tree: 6 5 4 3 2
Mirror tree will be:
5
/ \
6   3
/ \
4   2

Input:
2
/ \
1   8
/     \
12      9
Output:
Inorder of original tree: 12 1 2 8 9
Inorder of mirror tree: 9 8 2 1 12
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Write a recursive function that will take two nodes as the argument, one of the original tree and the other of the newly created tree. Now, for every passed node of the original tree, create a corresponding node in the mirror tree and then recursively call the same method for the child nodes but passing the left child of the original tree node with the right child of the mirror tree node and the right child of the original tree node with the left child of the mirror tree node.

Below is the implementation of the above approach:

## C

 `// C implementation of the approach ` `#include ` `#include ` ` `  `// A binary tree node has data, pointer to ` `// left child and a pointer to right child ` `typedef` `struct` `treenode { ` `    ``int` `val; ` `    ``struct` `treenode* left; ` `    ``struct` `treenode* right; ` `} node; ` ` `  `// Helper function that allocates a new node with the ` `// given data and NULL left and right pointers ` `node* createNode(``int` `val) ` `{ ` `    ``node* newNode = (node*)``malloc``(``sizeof``(node)); ` `    ``newNode->val = val; ` `    ``newNode->left = NULL; ` `    ``newNode->right = NULL; ` `    ``return` `newNode; ` `} ` ` `  `// Helper function to print Inorder traversal ` `void` `inorder(node* root) ` `{ ` `    ``if` `(root == NULL) ` `        ``return``; ` `    ``inorder(root->left); ` `    ``printf``(``"%d "``, root->val); ` `    ``inorder(root->right); ` `} ` ` `  `// mirrorify function takes two trees, ` `// original tree and a mirror tree ` `// It recurses on both the trees, ` `// but when original tree recurses on left, ` `// mirror tree recurses on right and ` `// vice-versa ` `void` `mirrorify(node* root, node** mirror) ` `{ ` `    ``if` `(root == NULL) { ` `        ``mirror = NULL; ` `        ``return``; ` `    ``} ` ` `  `    ``// Create new mirror node from original tree node ` `    ``*mirror = createNode(root->val); ` `    ``mirrorify(root->left, &((*mirror)->right)); ` `    ``mirrorify(root->right, &((*mirror)->left)); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``node* tree = createNode(5); ` `    ``tree->left = createNode(3); ` `    ``tree->right = createNode(6); ` `    ``tree->left->left = createNode(2); ` `    ``tree->left->right = createNode(4); ` ` `  `    ``// Print inorder traversal of the input tree ` `    ``printf``(``"Inorder of original tree: "``); ` `    ``inorder(tree); ` `    ``node* mirror = NULL; ` `    ``mirrorify(tree, &mirror); ` ` `  `    ``// Print inorder traversal of the mirror tree ` `    ``printf``(``"\nInorder of mirror tree: "``); ` `    ``inorder(mirror); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `import` `java.util.Comparator; ` ` `  `class` `GFG ` `{ ` ` `  `// A binary tree node has data, pointer to  ` `// left child and a pointer to right child  ` `static` `class` `node  ` `{  ` `    ``int` `val;  ` `    ``node left;  ` `    ``node right;  ` `}  ` ` `  `// Helper function that allocates  ` `// a new node with the given data  ` `// and null left and right pointers  ` `static` `node createNode(``int` `val)  ` `{  ` `    ``node newNode = ``new` `node();  ` `    ``newNode.val = val;  ` `    ``newNode.left = ``null``;  ` `    ``newNode.right = ``null``;  ` `    ``return` `newNode;  ` `}  ` ` `  `// Helper function to print Inorder traversal  ` `static` `void` `inorder(node root)  ` `{  ` `    ``if` `(root == ``null``)  ` `        ``return``;  ` `    ``inorder(root.left);  ` `    ``System.out.print(root.val);  ` `    ``inorder(root.right);  ` `}  ` ` `  `// mirrorify function takes two trees,  ` `// original tree and a mirror tree  ` `// It recurses on both the trees,  ` `// but when original tree recurses on left,  ` `// mirror tree recurses on right and  ` `// vice-versa  ` `static` `node mirrorify(node root)  ` `{  ` `    ``if` `(root == ``null``)  ` `    ``{  ` `        ``return` `null``;  ` `         `  `    ``}  ` ` `  `    ``// Create new mirror node from original tree node  ` `    ``node mirror = createNode(root.val);  ` `    ``mirror.right = mirrorify(root.left);  ` `    ``mirror.left = mirrorify(root.right); ` `    ``return` `mirror; ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String args[]) ` `{  ` ` `  `    ``node tree = createNode(``5``);  ` `    ``tree.left = createNode(``3``);  ` `    ``tree.right = createNode(``6``);  ` `    ``tree.left.left = createNode(``2``);  ` `    ``tree.left.right = createNode(``4``);  ` ` `  `    ``// Print inorder traversal of the input tree  ` `    ``System.out.print(``"Inorder of original tree: "``);  ` `    ``inorder(tree);  ` `    ``node mirror = ``null``;  ` `    ``mirror = mirrorify(tree);  ` ` `  `    ``// Print inorder traversal of the mirror tree  ` `    ``System.out.print(``"\nInorder of mirror tree: "``);  ` `    ``inorder(mirror);  ` `}  ` `} ` ` `  `// This code is contributed by Arnab Kundu `

## Python3

 `# Python3 implementation of the approach ` ` `  `# A binary tree node has data,  ` `# pointer to left child and  ` `# a pointer to right child  ` `# Linked list node  ` `class` `Node:  ` `    ``def` `__init__(``self``, data):  ` `        ``self``.data ``=` `data  ` `        ``self``.left ``=` `None` `        ``self``.right ``=` `None` ` `  `# Helper function that allocates  ` `# a new node with the given data  ` `# and None left and right pointers ` `def` `createNode(val): ` `    ``newNode ``=` `Node(``0``) ` `    ``newNode.val ``=` `val ` `    ``newNode.left ``=` `None` `    ``newNode.right ``=` `None` `    ``return` `newNode ` ` `  `# Helper function to print Inorder traversal ` `def` `inorder(root): ` `    ``if` `(root ``=``=` `None``): ` `        ``return` `    ``inorder(root.left) ` `    ``print``( root.val, end ``=` `" "``) ` `    ``inorder(root.right) ` ` `  `# mirrorify function takes two trees, ` `# original tree and a mirror tree ` `# It recurses on both the trees, ` `# but when original tree recurses on left, ` `# mirror tree recurses on right and ` `# vice-versa ` `def` `mirrorify(root, mirror): ` ` `  `    ``if` `(root ``=``=` `None``) : ` `        ``mirror ``=` `None` `        ``return` `mirror ` `     `  `    ``# Create new mirror node  ` `    ``# from original tree node ` `    ``mirror ``=` `createNode(root.val) ` `    ``mirror.right ``=` `mirrorify(root.left,  ` `                           ``((mirror).right)) ` `    ``mirror.left ``=` `mirrorify(root.right,  ` `                          ``((mirror).left)) ` `    ``return` `mirror ` ` `  `# Driver Code  ` `if` `__name__``=``=``'__main__'``:  ` ` `  `    ``tree ``=` `createNode(``5``) ` `    ``tree.left ``=` `createNode(``3``) ` `    ``tree.right ``=` `createNode(``6``) ` `    ``tree.left.left ``=` `createNode(``2``) ` `    ``tree.left.right ``=` `createNode(``4``) ` ` `  `    ``# Print inorder traversal of the input tree ` `    ``print``(``"Inorder of original tree: "``) ` `    ``inorder(tree) ` `    ``mirror ``=` `None` `    ``mirror ``=` `mirrorify(tree, mirror) ` ` `  `    ``# Print inorder traversal of the mirror tree ` `    ``print``(``"\nInorder of mirror tree: "``) ` `    ``inorder(mirror) ` ` `  `# This code is contributed by Arnab Kundu `

## C#

 `using` `System; ` ` `  `// c# implementation of the approach  ` ` `  `public` `class` `GFG ` `{ ` ` `  `// A binary tree node has data, pointer to  ` `// left child and a pointer to right child  ` `public` `class` `node ` `{ ` `    ``public` `int` `val; ` `    ``public` `node left; ` `    ``public` `node right; ` `} ` ` `  `// Helper function that allocates  ` `// a new node with the given data  ` `// and null left and right pointers  ` `public` `static` `node createNode(``int` `val) ` `{ ` `    ``node newNode = ``new` `node(); ` `    ``newNode.val = val; ` `    ``newNode.left = ``null``; ` `    ``newNode.right = ``null``; ` `    ``return` `newNode; ` `} ` ` `  `// Helper function to print Inorder traversal  ` `public` `static` `void` `inorder(node root) ` `{ ` `    ``if` `(root == ``null``) ` `    ``{ ` `        ``return``; ` `    ``} ` `    ``inorder(root.left); ` `    ``Console.Write(``"{0:D} "``, root.val); ` `    ``inorder(root.right); ` `} ` ` `  `// mirrorify function takes two trees,  ` `// original tree and a mirror tree  ` `// It recurses on both the trees,  ` `// but when original tree recurses on left,  ` `// mirror tree recurses on right and  ` `// vice-versa  ` `public` `static` `node mirrorify(node root) ` `{ ` `    ``if` `(root == ``null``) ` `    ``{ ` `        ``return` `null``; ` ` `  `    ``} ` ` `  `    ``// Create new mirror node from original tree node  ` `    ``node mirror = createNode(root.val); ` `    ``mirror.right = mirrorify(root.left); ` `    ``mirror.left = mirrorify(root.right); ` `    ``return` `mirror; ` `} ` ` `  `// Driver code  ` `public` `static` `void` `Main(``string``[] args) ` `{ ` ` `  `    ``node tree = createNode(5); ` `    ``tree.left = createNode(3); ` `    ``tree.right = createNode(6); ` `    ``tree.left.left = createNode(2); ` `    ``tree.left.right = createNode(4); ` ` `  `    ``// Print inorder traversal of the input tree  ` `    ``Console.Write(``"Inorder of original tree: "``); ` `    ``inorder(tree); ` `    ``node mirror = ``null``; ` `    ``mirror = mirrorify(tree); ` ` `  `    ``// Print inorder traversal of the mirror tree  ` `    ``Console.Write(``"\nInorder of mirror tree: "``); ` `    ``inorder(mirror); ` `} ` `} `

Output:

```Inorder of original tree: 2 3 4 5 6
Inorder of mirror tree: 6 5 4 3 2
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.