Given two binary tress. The problem is to check whether the two binary tress are mirrors of each other or not.
Mirror of a Binary Tree: Mirror of a Binary Tree T is another Binary Tree M(T) with left and right children of all non-leaf nodes interchanged.
Trees in the above figure are mirrors of each other.
We have discussed a recursive solution to check if two trees are mirror. In this post iterative solution is discussed.
Prerequisite: Iterative inorder tree traversal using stack
Approach: The following steps are:
- Perform iterative inorder traversal of one tree and iterative reverse inorder traversal of the other tree in parallel.
- During these two iterative traversals check that the corresponding nodes have the same value or not. If not same then they are not mirrors of each other.
- If values are same, then check whether at any point in the iterative inorder traversal one of the root becomes null and the other is not null. If this happens then they are not mirrors of each other. This check ensures whether they have the corresponding mirror structures or not.
- Otherwise, both the trees are mirror of each other.
Reverse inorder traversal is the opposite of inorder traversal. In this, the right subtree is traversed first, then root, and then the left subtree.
Time Complexity: O(n)
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Check if two trees are mirror of each other using level order traversal
- Check if two trees are Mirror | Set 2
- Check if two trees are Mirror
- Iterative function to check if two trees are identical
- Iterative Approach to check if two Binary Trees are Isomorphic or not
- Merge Two Binary Trees by doing Node Sum (Recursive and Iterative)
- Check if two given strings are isomorphic to each other
- Check whether two strings are anagram of each other
- Check if a given array contains duplicate elements within k distance from each other
- Check mirror in n-ary tree
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Total number of possible Binary Search Trees and Binary Trees with n keys
- Generic Trees(N-array Trees)
- Minimize Cash Flow among a given set of friends who have borrowed money from each other
- Convert a Binary Tree into its Mirror Tree
- Mirror of n-ary Tree
- Find mirror of a given node in Binary tree
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Create a mirror tree from the given binary tree
- Sum of the mirror image nodes of a complete binary tree in an inorder way