-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmirrorCore.c
More file actions
103 lines (95 loc) · 1.92 KB
/
Copy pathmirrorCore.c
File metadata and controls
103 lines (95 loc) · 1.92 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int input_count = 0;
int mirror_count = 0;
typedef struct TreeNode
{
int val;
int frontCount; //用来记录访问次数
struct TreeNode* parents;
struct TreeNode* left;
struct TreeNode* right;
}TreeNode;
TreeNode* initTree(TreeNode* root)
{
root->left = root->right = NULL;
root->val = 0;
root->frontCount = 0;
}
TreeNode* buildTree(char** input)
{
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
initTree(root);
char ch[10];
strcpy(ch, input[input_count++]);
if (strcmp(ch,"#") == 0)
{
root = NULL;
}
else
{
root->val = atoi(ch);
root->left = buildTree(input);
root->right = buildTree(input);
}
return root;
}
int judgeMirror(TreeNode* left, TreeNode* right)
{
if (left == NULL && right == NULL)
{
return 1;
}
if ((left == NULL && right != NULL) || (right == NULL && left != NULL))
{
return 0;
}
if (left->val != right->val)
{
return 0;
}
if (left->val == right->val)
{
if (judgeMirror(left->left, right->right) && judgeMirror(left->right, right->left))
{
return 1;
}
else
{
return 0;
}
}
}
void count_mirror(TreeNode* root)
{
if (root == NULL)
{
return;
}
else
{
if (judgeMirror(root->left, root->right))
{
mirror_count++;
}
count_mirror(root->left);
count_mirror(root->right);
}
}
int main(void)
{
char input[1000];
fgets(input, sizeof(input), stdin);
char* token = strtok(input, " \n");
char* inputs[100];
int n = 0;
while (token != NULL) {
inputs[n++] = token;
token = strtok(NULL, " \n");
}
TreeNode* root = buildTree(inputs);
count_mirror(root);
printf("%d", mirror_count);
return 0;
}