-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP699.cpp
55 lines (51 loc) · 930 Bytes
/
P699.cpp
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
struct Node {
Node *L, *R;
int val;
};
Node* read() {
int val; cin >> val;
if(val == -1)
return NULL;
Node *n = new Node();
n->val = val;
n->L = read();
n->R = read();
return n;
}
void write(Node *n, int *ret, int pos) {
ret[pos] += n->val;
if(n->L != NULL)
write(n->L, ret, pos-1);
if(n->R != NULL)
write(n->R, ret, pos+1);
}
void kill(Node *n) {
if(n->L != NULL)
kill(n->L);
if(n->R != NULL)
kill(n->R);
delete n;
}
int main() {
int ret[400]; // root start at 200.
for(int cas = 1; true; ++cas) {
// Read tree:
Node *root = read();
if(root == NULL)
return 0;
memset(ret, 0, 400*sizeof(int));
write(root, ret, 200);
cout << "Case " << cas << ":" << endl;
bool first = true;
FORI(400) {
if(ret[i] != 0) {
if(!first)
cout << " ";
cout << ret[i];
first = false;
}
}
cout << endl << endl;
kill(root);
}
}