- #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;
- }
The Falling Leaves 下落的树叶 UVA 699(二叉树)
最新推荐文章于 2021-05-01 17:03:54 发布