Given two Binary Trees we have to detect if the two trees are Isomorphic. Two trees are called isomorphic if one of them can be obtained from another by a series of flips, i.e. by swapping left and right children of a number of nodes. Any number of nodes at any level can have their children swapped.
Note: Two empty trees are isomorphic.
For example, the following two trees are isomorphic with the following sub-trees flipped: 2 and 3, NULL and 6, 7, and 8.
To solve the question mentioned above we traverse both the trees iteratively using level order traversal and store the levels in a queue data structure. There are following two conditions at each level:
- Value of nodes has to be same.
- Number of nodes at each level should be same.
Check the size of the queue to match the second condition mentioned above. Store the nodes of each level of the first tree as key with a value and for the second tree we will store all the nodes of a level in a vector. If the key is found we will decrease the value so as to keep track of how many nodes with the same value exists at a level. If value becomes zero that means the first tree has only this much number of nodes and we will remove it as a key. At the end of each level we will iterate through the array and check whether each value exists on the map or not. There will be three conditions:
- If the key is not found then the first tree doesn’t contain a node found in the second tree at the same level.
- If the key is found but the value becomes negative then the second tree has more nodes with the same value than first tree.
- If map size is not zero, this means there are some keys still left which means the first tree has a node which doesn’t match with any node in the second tree.
Below is the implementation of the above approach:
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.
- Iterative approach to check if a Binary Tree is Perfect
- Check for Symmetric Binary Tree (Iterative Approach)
- Iterative approach to check for children sum property in a Binary Tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Get level of a node in binary tree | iterative approach
- Deepest right leaf node in a binary tree | Iterative approach
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Deepest left leaf node in a binary tree | iterative approach
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Merge Two Binary Trees by doing Node Sum (Recursive and Iterative)
- Iterative function to check if two trees are identical
- Iterative method to check if two trees are mirror of each other
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Check if a Binary Tree is BST : Simple and Efficient Approach
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check if leaf traversal of two Binary Trees is same?
- Check whether the two Binary Search Trees are Identical or Not
- Inorder predecessor and successor for a given key in BST | Iterative Approach
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.