您的位置:首页 > 其它

The Falling Leaves 下落的树叶 UVA 699(二叉树)

2017-01-09 15:42 369 查看
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=10000;
int sum[maxn];
void buildtree(int t){
    int temp;cin>>temp;
    if(temp==-1)return ;
    sum[t]+=temp;
    buildtree(t-1);buildtree(t+1);
}
bool inti(){
    int temp;cin>>temp;
    if(temp==-1)return  false;         //空树
    int in=maxn/2;
    sum[in]+=temp;                       //整棵树的根的水平位置
    buildtree(in-1);buildtree(in+1);
    return true;
}
int main(){
    memset(sum,0,sizeof(sum));
    int count=0;
    while(inti()){
        cout<<"Case "<<++count<<":"<<endl;
        int temp=0;
        while(sum[temp]==0)temp++;               //找到最左边的叶子
        cout<<sum[temp];
        while(sum[++temp]!=0)
            cout<<" "<<sum[temp];                //最后一个数字后不能有空格
            cout<<"\n\n";
        memset(sum,0,sizeof(sum));
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: