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:
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 whether a binary tree is a full binary tree or not | Iterative Approach
- 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
- 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
- Get level of a node in binary tree | iterative approach
- Deepest left leaf node in a binary tree | iterative approach
- Deepest right leaf node in a binary tree | Iterative approach
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Check whether the two Binary Search Trees are Identical or Not
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Inorder predecessor and successor for a given key in BST | Iterative Approach
- Smallest number in BST which is greater than or equal to N ( Iterative Approach)
- Reverse a Linked List in groups of given size (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 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.