-
Notifications
You must be signed in to change notification settings - Fork 1
/
Isomorphic_or_not.cpp
78 lines (64 loc) · 1.45 KB
/
Isomorphic_or_not.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
using namespace std;
struct BNode
{
int data;
BNode *left;
BNode *right;
};
BNode *createNode(int data)
{
BNode *temp = new BNode();
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
void insertNode(BNode *root, int data)
{
queue<BNode *> nq;
nq.push(root);
while (!nq.empty()) {
root = nq.front();
nq.pop();
if (!root->left) {
root->left = createNode(data);
break;
} else {
nq.push(root->left);
}
if (!root->right) {
root->right = createNode(data);
break;
} else {
nq.push(root->right);
}
}
return;
}
bool isIsomorphic(Node *root1, Node *root2)
{
if (!root1 && !root2)
return true;
if ((!root1 && root2) || (root1 && !root2))
return false;
return isIsomorphic(root1->left, root2->left) && isIsomorphic(root1->right, root2->right);
}
int main()
{
Node *root1 = nullptr;
Node *root2 = nullptr;
// Tree 1
insertNode(root1, 100);
insertNode(root1, 200);
insertNode(root1, 3);
insertNode(root1, 2);
insertNode(root1, 7);
// Tree 2
insertNode(root2, 200);
insertNode(root2, 300);
insertNode(root2, 3);
insertNode(root2, 2);
insertNode(root2, 7);
(isIsomorphic(root1, root2)) ? cout << "Are Isomorphic.." << '\n' : cout << "Not Isomorphic" << '\n';
}