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

缺乏、安全感 2024-02-17 18:51 105阅读 0赞
  1. #include
  2. #include
  3. using namespace std;
  4. const int maxn=10000;
  5. int sum[maxn];
  6. void buildtree(int t){
  7. int temp;cin>>temp;
  8. if(temp==-1)return ;
  9. sum[t]+=temp;
  10. buildtree(t-1);buildtree(t+1);
  11. }
  12. bool inti(){
  13. int temp;cin>>temp;
  14. if(temp==-1)return false; //空树
  15. int in=maxn/2;
  16. sum[in]+=temp; //整棵树的根的水平位置
  17. buildtree(in-1);buildtree(in+1);
  18. return true;
  19. }
  20. int main(){
  21. memset(sum,0,sizeof(sum));
  22. int count=0;
  23. while(inti()){
  24. cout<<”Case “<<++count<<”:”<<endl;
  25. int temp=0;
  26. while(sum[temp]==0)temp++; //找到最左边的叶子
  27. cout<<sum[temp];
  28. while(sum[++temp]!=0)
  29. cout<<” “<<sum[temp]; //最后一个数字后不能有空格
  30. cout<<”\n\n”;
  31. memset(sum,0,sizeof(sum));
  32. }
  33. return 0;
  34. }

发表评论

表情:
评论列表 (有 0 条评论,105人围观)

还没有评论,来说两句吧...

相关阅读

    相关 S-Trees UVA712(

    题目读了半天,差点被吓住!题目本身很简单,就是一颗满二叉树,向左(2\temp),向右(2\temp+1),最后减去(1<<n)-1;(即非叶子结点的个数,因为储存叶子结点是从