-
Notifications
You must be signed in to change notification settings - Fork 4
/
identical_tree_recursive.C
55 lines (47 loc) · 1.12 KB
/
identical_tree_recursive.C
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
//author @ Ayush Aggarwal
// Recursive solution
//check whether two trees are identical or not
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *left,*right;
};
struct node* newNode(int val)
{
struct node *temp = new node;
temp->data = val;
temp->left = NULL;
temp->right = NULL;
return temp;
}
bool check_identical(struct node *root1, struct node *root2)
{
if(!root1 and !root2)
return true;
if(!root1 or !root2)
return false;
if((root1->data==root2->data) and (check_identical(root1->left,root2->left)) and (check_identical(root1->right,root2->right)))
return true;
else
return false;
}
int main()
{
struct node *root1 = newNode(1);
root1->left = newNode(2);
root1->right = newNode(3);
root1->left->left = newNode(4);
root1->left->right = newNode(5);
struct node *root2 = newNode(1);
root2->left = newNode(2);
root2->right = newNode(3);
root2->left->left = newNode(4);
root2->left->right = newNode(5);
if(check_identical(root1,root2))
cout<<" Both trees are identical";
else
cout<<" Trees are not identical";
return 0;
}