# Iterative Approach to check if two Binary Trees are Isomorphic or not

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. Approach:

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:

 `// C++ program to find two ` `// Binary Tree are Isomorphic or not ` ` `  `#include ` `using` `namespace` `std; ` ` `  `/* A binary tree node has data, ` `pointer to left and right children */` `struct` `node { ` `    ``int` `data; ` `    ``struct` `node* left; ` `    ``struct` `node* right; ` `}; ` ` `  `/* function to retun if ` `   ``tree are isomorphic or not*/` `bool` `isIsomorphic(node* root1, node* root2) ` `{ ` `    ``// if Both roots are null ` `    ``// then tree is isomorphic ` `    ``if` `(root1 == NULL and root2 == NULL) ` `        ``return` `true``; ` ` `  `    ``// check if one node is false ` `    ``else` `if` `(root1 == NULL or root2 == NULL) ` `        ``return` `false``; ` ` `  `    ``queue q1, q2; ` ` `  `    ``// enqueue roots ` `    ``q1.push(root1); ` `    ``q2.push(root2); ` ` `  `    ``int` `level = 0; ` `    ``int` `size; ` ` `  `    ``vector<``int``> v2; ` ` `  `    ``unordered_map<``int``, ``int``> mp; ` ` `  `    ``while` `(!q1.empty() and !q2.empty()) { ` ` `  `        ``// check if no. of nodes are ` `        ``// not same at a given level ` `        ``if` `(q1.size() != q2.size()) ` `            ``return` `false``; ` ` `  `        ``size = q1.size(); ` ` `  `        ``level++; ` ` `  `        ``v2.clear(); ` `        ``mp.clear(); ` ` `  `        ``while` `(size--) { ` ` `  `            ``node* temp1 = q1.front(); ` `            ``node* temp2 = q2.front(); ` ` `  `            ``// dequeue the nodes ` `            ``q1.pop(); ` `            ``q2.pop(); ` ` `  `            ``// check if value ` `            ``// exists in the map ` `            ``if` `(mp.find(temp1->data) == mp.end()) ` `                ``mp[temp1->data] = 1; ` ` `  `            ``else` `                ``mp[temp1->data]++; ` ` `  `            ``v2.push_back(temp2->data); ` ` `  `            ``// enqueue the child nodes ` `            ``if` `(temp1->left) ` `                ``q1.push(temp1->left); ` ` `  `            ``if` `(temp1->right) ` `                ``q1.push(temp1->right); ` ` `  `            ``if` `(temp2->left) ` `                ``q2.push(temp2->left); ` ` `  `            ``if` `(temp2->right) ` `                ``q2.push(temp2->right); ` `        ``} ` ` `  `        ``// Iterate through each node at a level ` `        ``// to check whether it exists or not. ` `        ``for` `(``auto` `i : v2) { ` ` `  `            ``if` `(mp.find(i) == mp.end()) ` `                ``return` `false``; ` ` `  `            ``else` `{ ` `                ``mp[i]--; ` ` `  `                ``if` `(mp[i] < 0) ` `                    ``return` `false``; ` ` `  `                ``else` `if` `(mp[i] == 0) ` `                    ``mp.erase(i); ` `            ``} ` `        ``} ` ` `  `        ``// check if the key remain ` `        ``if` `(mp.size() != 0) ` `            ``return` `false``; ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `/* function that allocates a new node with the  ` `given data and NULL left and right pointers. */` `node* newnode(``int` `data) ` `{ ` `    ``node* temp = ``new` `node; ` `    ``temp->data = data; ` `    ``temp->left = NULL; ` `    ``temp->right = NULL; ` ` `  `    ``return` `(temp); ` `} ` ` `  `/* Driver program*/` ` `  `int` `main() ` `{ ` `    ``// create tree ` `    ``struct` `node* n1 = newnode(1); ` `    ``n1->left = newnode(2); ` `    ``n1->right = newnode(3); ` `    ``n1->left->left = newnode(4); ` `    ``n1->left->right = newnode(5); ` `    ``n1->right->left = newnode(6); ` `    ``n1->left->right->left = newnode(7); ` `    ``n1->left->right->right = newnode(8); ` ` `  `    ``struct` `node* n2 = newnode(1); ` `    ``n2->left = newnode(3); ` `    ``n2->right = newnode(2); ` `    ``n2->right->left = newnode(4); ` `    ``n2->right->right = newnode(5); ` `    ``n2->left->right = newnode(6); ` `    ``n2->right->right->left = newnode(8); ` `    ``n2->right->right->right = newnode(7); ` ` `  `    ``if` `(isIsomorphic(n1, n2) == ``true``) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

Output:

```Yes
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.