-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchSameTree.cpp
More file actions
54 lines (41 loc) · 1.08 KB
/
BinarySearchSameTree.cpp
File metadata and controls
54 lines (41 loc) · 1.08 KB
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
#include<iostream>
#include <cstdlib>
using namespace std;
struct node{
int data;
struct node *left;
struct node *right;
};
struct node* newNode(int data) {
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
bool isSameTree(node *root1 ,node *root2){
if(root1 == NULL && root2 == NULL){
return true;
}
if(root1 == NULL || root2 == NULL){
return false;
}
return root1->data == root2->data && isSameTree(root1->left,root2->left)&&isSameTree(root1->right , root2->right);
}
int main(){
Tree One
struct node* root1 = newNode(10);
root1->left = newNode(16);
root1->right = newNode(15);
root1->right->left = newNode(18);
Tree Two
struct node* root2 = newNode(10);
root2->left = newNode(16);
root2->right = newNode(15);
root2->right->left = newNode(18);
if(isSameTree(root1,root2)){
std::cout<<"Tree Matched "<<endl;
}else{
std::cout<<"Both The tree is different"<<endl;
}
}