问题描述
- 初学二叉树 运行有错误 瞅了几个小时无果 大神帮帮忙 找出错误在哪
-
![图片说明](http://img.ask.csdn.net/upload/201503/07/1425715444_303984.png#include
#includetypedef struct Tree
{
int date;
struct Tree *lson,*rson;
}tree , *ztree;int a[200005];
ztree creat(ztree t,int n)
{
int h;
if(n==0)
return 0;
scanf("%d",&h);
if(!(t=(tree*)malloc(sizeof(tree))))
{
printf("内存空间不足");
}
else
{
if(h!=0)
{
t->date=h;
t->lson=NULL;
t->rson=NULL;
}
else
{
t->date=0;
creat(t->lson,n--);
creat(t->rson,n--);
}
}
return t;
}int swap(ztree t)
{
int sum1=0,sum2=0,sum=0;
ztree x;
if(t->date!=0)
{
sum=t->date;
return sum;
}
else
{
sum1+=swap(t->lson);
sum2+=swap(t->rson);
if(sum1>sum2)
{
x=t->lson;
t->lson=t->rson;
t->rson=x;
}
return (sum1+sum2)/2;
}
}int i=1;
void find(ztree t)
{
if(t->date==0)
{
find(t->lson);
find(t->rson);
}
else
{
a[i++]=t->date;
}
return;
}int sort()
{
int j,k,sum=0;
for(j=1;j
{
for(k=j+1;k
{
if(a[j]>a[k])
sum++;
}
}
return printf("%d",sum);
}int main()
{
ztree t=NULL;
int n;
scanf("%d",&n);
t=creat(t,2*n-1);
swap(t);
find(t);
sort(t);
}