-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestion34.c
108 lines (89 loc) · 1.76 KB
/
question34.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
http://practice.geeksforgeeks.org/problems/two-mirror-trees/1
Check if one tree is mirror of another
*/
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *left;
struct node *right;
};
struct node *newNode(int data){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
int checkMirror(struct node *root1, struct node *root2){
if((!root1 && root2) || (!root2 && root1)){
return 0;
}
if(!root1 && !root2){
return 1;
}
if(root1->data != root2->data){
return 0;
}
return checkMirror(root1->left, root2->right) && checkMirror(root1->right, root2->left);
}
void preorder(struct node *root,int a,int b,int c){
if(root){
if(root->data == a){
if(c == 'L'){
root->left = newNode(b);
return;
}else{
root->right = newNode(b);
return;
}
}
preorder(root->left,a,b,c);
preorder(root->right,a,b,c);
}
}
void inorder(struct node *root){
if(root){
inorder(root->left);
printf("%d\n", root->data);
inorder(root->right);
}
}
struct node *insertNode(struct node *temp, int a, int b, char c){
if(!temp){
temp = newNode(a);
if(c == 'L'){
temp->left = newNode(b);
}else{
temp->right = newNode(b);
}
}else{
preorder(temp, a,b,c);
}
return temp;
}
int main(){
int cases;
scanf("%d",&cases);
int i;
for(i=0;i<cases;i++){
int first,second;
struct node *tree1;
struct node *tree2;
scanf("%d %d",&first, &second);
int j;
char c;
int a,b;
for(j=0;j<first;j++){
scanf("%d %d %c",&a, &b, &c);
tree1 = insertNode(tree1,a,b,c);
}
for(j=0;j<second;j++){
scanf("%d %d %c",&a, &b, &c);
tree2 = insertNode(tree2,a,b,c);
}
printf("%d\n",checkMirror(tree1, tree2));
}
return 0;
}