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 firstname.lastname@example.org. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if two trees are Mirror
- Check if two trees are Mirror | Set 2
- Check if two trees are mirror of each other using level order traversal
- 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 mirror in n-ary tree
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Check for Symmetric Binary Tree (Iterative Approach)
- Iterative approach to check if a Binary Tree is Perfect
- Iterative approach to check for children sum property in a Binary Tree
- Check if two trees have same structure
- Check if all levels of two trees are anagrams or not
- Check if leaf traversal of two Binary Trees is same?
- Iterative Method to find Height of Binary Tree
- Iterative method to find ancestors of a given binary tree
- Iterative Method To Print Left View of a Binary Tree
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Generic Trees(N-array Trees)