输入样例之后用广搜打了一下插入的部分应该是没有问题的,但是层数的地方不对,求层数的函数不太会写,按照我的理解写出了一个就是mid这个函数
感谢愿意看我的垃圾程序的大佬!
#include<stdio.h>
#include<math.h>
struct tree{
int layer;
struct tree *lc,*rc;
}shu[100000];
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int ceng;
void mid(struct tree *t,int layer)
{
if(t!=NULL)
{
mid(t->lc,++layer);
if(1)
{
ceng=max(ceng,layer);
}
mid(t->rc,++layer);
}
}
#define maxsize 100
void layerr(struct tree *t)
{
struct tree *dui[maxsize],*p;
int front,rear;
if(t!=NULL)
{
dui[0]=t;
front=0;
rear=0;
while(front<=rear)
{
p=dui[front++];
if(1)
{
printf("%d ",p->layer);
}
if(p->lc!=NULL)
{
dui[++rear]=p->lc;
}
if(p->rc!=NULL)
{
dui[++rear]=p->rc;
}
}
}
}
int main()
{
int n;
scanf("%d",&n);
int a,b;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
shu[i].layer=i;
if(a!=0)
shu[i].lc=&shu[a];
else
shu[i].lc=NULL;
if(b!=0)
shu[i].rc=&shu[b];
else
shu[i].rc=NULL;
}
int layer=0;
mid(&shu[1],layer);
//printf("\n");
//layerr(&shu[1]);
printf("%d",ceng);
}