[cpp]
/**題目大意:先序建立二叉樹,求同一垂直線上葉子節點之和。
*模擬先序建立二叉樹過程,用數組保存值,數組下標代表垂直坐標求解
*/
#include <cstdio> www.2cto.com
#include <cstring>
using namespace std;
struct Node {
int v, pos;
Node *lc, *rc;
};
#define MID 81
int a[MID*2];
int pre_create_tree(int pos) {
int v;
scanf("%d", &v);
if( -1 == v ) return 1;
a[MID+pos] += v;
pre_create_tree(pos-1);
pre_create_tree(pos+1);
return 0;
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int flag, i(1);
while(true) {
flag = 0;
memset(a, 0, sizeof(a));
if( pre_create_tree(0) ) break;
printf("Case %d:\n", i++);
for(int i = 0; i < MID*2; i ++) {
if( !a[i] ) continue;
if( !flag ) {printf("%d", a[i]); flag = 1;}
else printf(" %d", a[i]);
} www.2cto.com
printf("\n\n");
}
return 0;
}