-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeMirrorImage.c
More file actions
82 lines (55 loc) · 1.36 KB
/
treeMirrorImage.c
File metadata and controls
82 lines (55 loc) · 1.36 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
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
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *left, *right;
} Node;
Node* newNode(int data) {
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
int isEqual(Node* a, Node* b) {
if (a == NULL && b == NULL)
return 1;
if (a == NULL || b == NULL)
return 0;
return (a->data == b->data) &&
isEqual(a->left, b->left) &&
isEqual(a->right, b->right);
}
void mirror(Node* root) {
if (!root) return;
Node* temp = root->left;
root->left = root->right;
root->right = temp;
mirror(root->left);
mirror(root->right);
}
void inorder(Node* root) {
if (!root) return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
int main() {
Node* root1 = newNode(1);
root1->left = newNode(2);
root1->right = newNode(3);
Node* root2 = newNode(1);
root2->left = newNode(2);
root2->right = newNode(3);
if (isEqual(root1, root2))
printf("Trees are equal\n");
else
printf("Trees are NOT equal\n");
printf("Inorder of Tree1 before mirroring: ");
inorder(root1);
printf("\n");
mirror(root1);
printf("Inorder of Tree1 after mirroring: ");
inorder(root1);
printf("\n");
return 0;
}