回答編集履歴
1
補足を受けての追記
test
CHANGED
@@ -1,3 +1,109 @@
|
|
1
1
|
全ノードを、根から黒ノードの数を数えながら巡って、葉にたどり着いたら、黒接点の数を記録しておき、すでに記録した物と異なれば`0`を返して終了し、全部のノードを巡り終わったら`1`を返す。
|
2
2
|
|
3
3
|
再帰呼び出しで巡れば難しくない気がします。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
#追記
|
8
|
+
|
9
|
+
黒の数には、根と葉も入れるとして、こんな感じでしょうか。
|
10
|
+
|
11
|
+
```C
|
12
|
+
|
13
|
+
##include <stdio.h>
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
typedef struct node { struct node *left, *right; int black; } NODE;
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
int count_save;
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
int check(NODE *this, int count);
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
int main(void){
|
30
|
+
|
31
|
+
NODE *root;
|
32
|
+
|
33
|
+
NODE x1, x2, x3, x4, x5, x6;
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
root = &x1;
|
38
|
+
|
39
|
+
x1 = (NODE){ &x2, &x3, 0 };
|
40
|
+
|
41
|
+
x2 = (NODE){ &x4, NULL, 1 };
|
42
|
+
|
43
|
+
x3 = (NODE){ &x5, &x6, 0 };
|
44
|
+
|
45
|
+
x4 = (NODE){ NULL, NULL, 0 };
|
46
|
+
|
47
|
+
x5 = (NODE){ NULL, NULL, 1 };
|
48
|
+
|
49
|
+
x6 = (NODE){ NULL, NULL, 1 };
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
count_save = -1;
|
54
|
+
|
55
|
+
printf("%d\n",check(root, 0));
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
int check(NODE *this, int count){
|
62
|
+
|
63
|
+
count += this->black;
|
64
|
+
|
65
|
+
if(!this->left && !this->right){ /* Leaf */
|
66
|
+
|
67
|
+
if(count_save==-1){
|
68
|
+
|
69
|
+
count_save = count;
|
70
|
+
|
71
|
+
return 1;
|
72
|
+
|
73
|
+
}else if(count_save != count){
|
74
|
+
|
75
|
+
return 0;
|
76
|
+
|
77
|
+
}else{
|
78
|
+
|
79
|
+
return 1;
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
if(this->left){
|
86
|
+
|
87
|
+
if(check(this->left,count)==0){
|
88
|
+
|
89
|
+
return 0;
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
if(this->right){
|
96
|
+
|
97
|
+
if(check(this->right,count)==0){
|
98
|
+
|
99
|
+
return 0;
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
return 1;
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
```
|