我们可以把它直接考虑是个一维数组,对每个点向两边扩展,记录数据在数组中,
就是DFS
#include <stdio.h> int a[81],left,right; void sort(int num,int pos) {int x,y; if (num!=-1) {if (pos<left) left=pos; if (pos>right) right=pos; a[pos]+=num; scanf("%d",&x); sort(x,pos-1); scanf("%d",&y); sort(y,pos+1); } }; int main() {int i,n,sum=0; while (scanf("%d",&n)&&n!=-1) {for (i=0;i<81;i++) a[i]=0; ++sum; left=40; right=40; sort(n,40); printf("Case %d:\n",sum); for (i=left;i<right;i++) printf("%d ",a[i]); printf("%d\n\n",a[right]); } return 0; }
原文地址:http://blog.csdn.net/asuxiexie/article/details/38051613