Tree Isomorphism Problem
Write a function to detect if two trees are isomorphic. Two trees are called isomorphic if one of them can be obtained from other 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. Two empty trees are isomorphic.
For example, following two trees are isomorphic with following subtrees flipped: 2 and 3, NULL and 6, 7 and 8.
We simultaneously traverse both trees. Let the current internal nodes of two trees being traversed be n1 and n2 respectively. There are following two conditions for subtrees rooted with n1 and n2 to be isomorphic.
1) Data of n1 and n2 is same.
2) One of the following two is true for children of n1 and n2
……a) Left child of n1 is isomorphic to left child of n2 and right child of n1 is isomorphic to right child of n2.
……b) Left child of n1 is isomorphic to right child of n2 and right child of n1 is isomorphic to left child of n2.
 C++

// A C++ program to check if two given trees are isomorphic
#include <iostream>
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;
};
/* Given a binary tree, print its nodes in reverse level order */
bool
isIsomorphic(node* n1, node *n2)
{
// Both roots are NULL, trees isomorphic by definition
if
(n1 == NULL && n2 == NULL)
return
true
;
// Exactly one of the n1 and n2 is NULL, trees not isomorphic
if
(n1 == NULL  n2 == NULL)
return
false
;
if
(n1>data != n2>data)
return
false
;
// There are two possible cases for n1 and n2 to be isomorphic
// Case 1: The subtrees rooted at these nodes have NOT been "Flipped".
// Both of these subtrees have to be isomorphic, hence the &&
// Case 2: The subtrees rooted at these nodes have been "Flipped"
return
(isIsomorphic(n1>left,n2>left) && isIsomorphic(n1>right,n2>right))
(isIsomorphic(n1>left,n2>right) && isIsomorphic(n1>right,n2>left));
}
/* Helper 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 to test above functions*/
int
main()
{
// Let us create trees shown in above diagram
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
Time Complexity: The above solution does a traversal of both trees. So time complexity is O(m + n) where m and n are number of nodes in given trees.
Disclaimer: This does not belong to TechCodeBit, its an article taken from the below
source and credits.
source and credits: http://www.geeksforgeeks.org
We have built the accelerating growthoriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at
http://www.techcodebit.com. #techcodebit #google #microsoft #facebook #interview portal #jobplacements
#technicalguide