-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeIdentical.cpp
49 lines (41 loc) · 1.03 KB
/
BinaryTreeIdentical.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
#include<bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node* left;
node* right;
};
node* newNode(int data)
{
node* Node = new node();
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return(Node);
}
int identicaltrees(node * a , node * b){
if( a == NULL && b == NULL){
return 1 ;
}
if( a != NULL && b != NULL){
return(a->data == b->data && identicaltrees(a->left,b->left) && identicaltrees(a->right,b->right));
}
return 0 ;
}
int main(){
node *root1 = newNode(1);
node *root2 = newNode(1);
root1->left = newNode(2);
root1->right = newNode(3);
root1->left->left = newNode(4);
root1->left->right = newNode(5);
root2->left = newNode(2);
root2->right = newNode(3);
root2->left->left = newNode(4);
root2->left->right = newNode(5);
string ans = identicaltrees(root1 , root2) ? "Identical Trees" : "Non Identical Trees" ;
cout << ans ;
return 0 ;
}