-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidenticalTrees.c
More file actions
46 lines (42 loc) · 1 KB
/
identicalTrees.c
File metadata and controls
46 lines (42 loc) · 1 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
#include <stdio.h>
#include <conio.h>
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);
}
int identical(struct node *root1, struct node *root2){
if((root1 == NULL) && (root2 == NULL))
return 1;
if((root1->data == root2->data)){
return (identical(root1->left, root2->left) && identical(root1->right, root2->right));
}
return 0;
}
int main(){
struct node *root1, *root2;
int idenTree;
root1 = newNode(1);
root1->left = newNode(2);
root1->right = newNode(3);
root1->left->left = newNode(4);
root1->left->left->right = newNode(5);
root2 = newNode(1);
root2->left = newNode(2);
root2->right = newNode(3);
root2->left->left = newNode(4);
root2->left->left->right = newNode(5);
idenTree = identical(root1, root2);
if(idenTree == 0)
printf("Trees are not identical");
else
printf("Identical Trees");
return 0;
}