-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckTreesIdentical.cpp
111 lines (107 loc) · 2.44 KB
/
CheckTreesIdentical.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Implemented by Kritagya Kumra
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
node(int d)
{
this->data = d;
this->left = NULL;
this->right = NULL;
}
};
bool isIdentical(node *root1, node *root2)
{
// Time Complexity is O(n^2)
// Time Complexity is O(height)
if ((root1 == NULL && root2 != NULL) || (root2 == NULL && root1 != NULL))
{
return false;
}
if (root1 == NULL && root2 == NULL)
{
return true;
}
bool leftAns = isIdentical(root1->left, root2->left);
bool rightAns = isIdentical(root1->left, root2->left);
bool mainCondition = root1->data == root2->data;
if (leftAns && rightAns && mainCondition)
{
return true;
}
else
{
return false;
}
}
bool isIdenticalIterative(node *root1, node *root2)
{
// Time Complexity :- O(n)
// Time Complexity is O(height)
queue<node *> q1;
queue<node *> q2;
q1.push(root1);
q2.push(root2);
while (!q1.empty() && !q2.empty())
{
node *curr1 = q1.front();
node *curr2 = q2.front();
q1.pop();
q2.pop();
if (curr1->data != curr2->data)
{
return false;
}
if (curr1->left != NULL && curr2->left != NULL)
{
q1.push(curr1->left);
q2.push(curr2->left);
}
if (curr1->right != NULL && curr2->right != NULL)
{
q1.push(curr1->right);
q2.push(curr2->right);
}
}
if (q1.size() == q2.size())
{
return true;
}
else
{
return false;
}
}
int main()
{
node *root1 = new node(10);
root1->left = new node(8);
root1->right = new node(2);
root1->left->left = new node(3);
root1->left->right = new node(5);
root1->right->left = new node(2);
root1->right->left->right = new node(12);
root1->right->left->right->left = new node(11);
node *root = new node(10);
root->left = new node(20);
root->right = new node(30);
root->left->left = new node(40);
root->left->right = new node(60);
// bool ans = isBalanced(root);
bool ans = isIdenticalIterative(root, root);
if (ans == true)
{
cout << "Both Binary Tree are identical ";
}
else
{
cout << "Both Binary Tree are not identical ";
}
}
// Implemented by Kritagya Kumra