-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolourTree.c
More file actions
126 lines (111 loc) · 2.77 KB
/
Copy pathcolourTree.c
File metadata and controls
126 lines (111 loc) · 2.77 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <stdio.h>
#include <stdlib.h>
int sum = 0;
struct TreeNode {
int val;
int color;
struct TreeNode *left;
struct TreeNode *right;
};
/******************* 染色 *******************/
void visitTree(struct TreeNode *root,int min, int max,int color)
{
if (root == NULL)
{
return;
}
else
{
if (min <= root->val && root->val <= max) //染色
{
root->color = color;
}
visitTree(root->left,min,max,color); //递归访问左右子树
visitTree(root->right,min,max,color);
}
}
void ColorNum(struct TreeNode *root)
{
if (root == NULL)
{
return;
}
else
{
if (root->color == 1)
{
sum++;
}
ColorNum(root->left);
ColorNum(root->right);
}
}
void getNumber(struct TreeNode* root, int** ops, int opsSize){
int min, max, color;
for (int i = 0; i < opsSize; i++)
{
color = ops[i][0];
min = ops[i][1];
max = ops[i][2];
visitTree(root,min,max,color);
}
}
/*****************************************************/
/******************* 读取数据 *******************/
struct TreeNode* newTreeNode(int val) {
struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
node->val = val;
node->color = 0;
node->left = node->right = NULL;
return node;
}
struct TreeNode* constructTree(int size) {
if (size == 0)
return NULL;
struct TreeNode** nodes = (struct TreeNode**)malloc(size * sizeof(struct TreeNode*));
for (int i = 0; i < size; i++) {
int val;
scanf("%d", &val);
if (val == -1) {
nodes[i] = NULL;
} else {
nodes[i] = newTreeNode(val);
}
}
for (int i = 0, j = 1; j < size; i++) {
if (nodes[i] != NULL) {
if (j < size)
nodes[i]->left = nodes[j++];
if (j < size)
nodes[i]->right = nodes[j++];
}
}
struct TreeNode* root = nodes[0];
free(nodes);
return root;
}
void readOps(int ***ops, int *opsSize) {
scanf("%d", opsSize);
*ops = (int **)malloc(*opsSize * sizeof(int *));
while(getchar() != '[') {}
for (int i = 0; i < *opsSize; i++) {
(*ops)[i] = (int *)malloc(3 * sizeof(int));
while(getchar() != '[') {}
for (int j = 0; j < 3; j++) {
scanf("%d", &((*ops)[i][j]));
}
while(getchar() != ']') {}
}
}
/*****************************************************/
int main() {
int nodeSize;
scanf("%d", &nodeSize);
struct TreeNode* root = constructTree(nodeSize);
int **ops, opsSize;
readOps(&ops, &opsSize);
getNumber(root, ops, opsSize);
ColorNum(root);
printf("%d",sum);
return 0;
}