Given a binary tree, the task is to create a new binary tree which is a mirror image of the given binary tree.
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
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:
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.
- Convert a Binary Tree into its Mirror Tree
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Find mirror of a given node in Binary tree
- Sum of the mirror image nodes of a complete binary tree in an inorder way
- Number of edges in mirror image of Complete binary tree
- Symmetric Tree (Mirror Image of itself)
- Mirror of n-ary Tree
- Check mirror in n-ary tree
- Check if the given two matrices are mirror images of one another
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Check whether a given binary tree is skewed binary tree or not?
- Create a sorted linked list from the given Binary Tree
- Check if two trees are Mirror
- Iterative method to check if two trees are mirror of each other
- Check if two trees are Mirror | Set 2
- Check if two trees are mirror of each other using level order traversal
- Print sum of matrix and its mirror image
- Check if a binary tree is subtree of another binary tree | Set 1
- Binary Tree to Binary Search Tree Conversion
- Check if a binary tree is subtree of another binary tree | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.