/
identical.cpp
57 lines (49 loc) · 1.25 KB
/
identical.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
#include <iostream>
using std :: cout;
using std :: cin;
using std :: endl;
struct node
{
int data;
struct node * left;
struct node * right;
};
struct node * newNode (int x)
{
struct node * node = new struct node;
node -> data = x;
node -> left = NULL;
node -> right = NULL;
return node;
}
bool isIdentical (struct node * root1, struct node * root2)
{
if (root1 == NULL && root2 == NULL)
return true;
if (root1 != NULL && root2 != NULL)
return (root1 -> data == root2 -> data && isIdentical (root1 -> left, root2 -> left)
&& isIdentical (root1 -> right, root2 -> right));
return false;
}
int main (void)
{
struct node * root1 = newNode (1);
root1 -> left = newNode (2);
root1 -> right = newNode (3);
root1 -> left -> left = newNode (4);
root1 -> left -> right = newNode (5);
root1 -> right -> left = newNode (6);
root1 -> right -> right = newNode (7);
struct node * root2 = newNode (1);
root2 -> left = newNode (2);
root2 -> right = newNode (3);
root2 -> left -> left = newNode (4);
root2 -> left -> right = newNode (5);
root2 -> right -> left = newNode (6);
root2 -> right -> right = newNode (7);
if (isIdentical (root1, root2))
cout << "Trees are identical." << endl;
else
cout << "Trees are not identical." << endl;
return 0;
}