您的位置:首页 > 其它

UVA - 699

2014-11-26 10:51 148 查看
The Falling Leaves
Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how large would the piles of leaves become?

We assume each node in a binary tree "drops" a number of leaves equal to the integer value stored in that node. We also assume that these leaves drop vertically to the ground (thankfully, there's no wind to blow them around). Finally, we assume that the nodes are positioned horizontally in such a manner that the left and right children of a node are exactly one unit to the left and one unit to the right, respectively, of their parent. Consider the following tree:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

void create(int num,int* a) {
int data;
cin >> data;
if (data == -1) return;
a[num] += data;
create(num - 1,a);
create(num + 1,a);
}

int main () {
int head;
int Case = 1;
freopen("1.in","r",stdin);
while (1) {
int ans[200];
memset(ans,0,sizeof(ans));
create(90,ans);
int pos = 0;
for (int i = 0;i < 200;i++) {
if (ans[i] != 0) ans[pos++] = ans[i];
}
if (pos == 0) break;
printf("Case %d:\n",Case++);
for (int i =0 ;i < pos - 1;i++) cout << ans[i] <<" ";
cout << ans[pos - 1] << endl<< endl;
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: