forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
101.c
76 lines (58 loc) · 1.45 KB
/
101.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
bool isSymmetric0(struct TreeNode* left, struct TreeNode* right){
if (left == NULL && right == NULL) return true;
if (left && right
&& (left->val == right->val)
&& isSymmetric0(left->left, right->right)
&& isSymmetric0(left->right, right->left))
return true;
else
return false;
}
bool isSymmetric(struct TreeNode* root) {
if (root == NULL) return true;
return isSymmetric0(root->left, root->right);
}
void printTreePreOrder(struct TreeNode* root) {
if (root) {
printf("%d", root->val);
printTreePreOrder(root->left);
printTreePreOrder(root->right);
}
else
printf("#");
}
int main() {
struct TreeNode* r = (struct TreeNode*)calloc(7, sizeof(struct TreeNode));
struct TreeNode* p = r;
p->val = 1;
p->left = r + 1;
p->right = r + 2;
p = r + 1;
p->val = 2;
p->left = r + 3;
p->right = r + 4;
p = r + 2;
p->val = 2;
p->left = r + 5;
p->right = r + 6;
p = r + 3;
p->val = 3;
p = r + 4;
p->val = 4;
p = r + 5;
p->val = 4;
p = r + 6;
p->val = 3;
printTreePreOrder(r); printf("\n");
/* should be true */
printf("%d\n", isSymmetric(r));
return 0;
}