问题连接:https://odzkskevi.qnssl.com/c6558ab5e65b2703fade6a22971fa34a?v=1535080290
代码:
#define N 200
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int k[N];
struct Node
{
int Data;
struct Node *LChild;
struct Node *RChild;
};
void Crate(struct Node *bt,int dis)//在建树的同时保留每列相对的节点总和
{
int a;
scanf("%d",&a);//不存在这个节点
if(a==-1){bt=NULL;return;}
else
{
k[dis]+=a;//节点总和
bt=(struct Node *)malloc(sizeof(struct Node));
bt->Data=a;
Crate(bt->LChild,dis-1);
Crate(bt->RChild,dis+1);
}
}
int main()
{
int a,Case=1;
while(scanf("%d",&a)&&a!=-1)
{
memset(k,0,sizeof(k));
struct Node *bt;bt=(struct Node *)malloc(sizeof(struct Node));
bt->Data=a;k[85]+=a;//假设根节点在数组85的位置
Crate(bt->LChild,85-1);//左节点在根节点左边一个位置,右节点在右边一个位置
Crate(bt->RChild,85+1);
printf("Case %d:\n",Case++);
int f=0;
for(int i=0; i<N; i++)
{
if(k[i])
{
if(f)
printf(" %d",k[i]);
else
{
printf("%d",k[i]);f=1;
}
}
}
printf("\n\n");
}
return 0;
}