#include "stdlib.h"
#include "stdio.h"
#include"conio.h"
#define NULL 0
typedef struct stu
{
char data;
struct stu *left,*right;
}sn;
sn *Create(sn *a)
{
char ch;
scanf("%c",&ch);
if(ch=='*') a=NULL;
else
{
a=(sn *)malloc(sizeof(sn));
if (!a) printf("yuguyuy");
a->data=ch;
a->left=Create(a->left);
a->right=Create(a->right);
}
return(a);
}
void inc(sn *b)
{
if(b)
{
inc(b->left);
printf("%c",b->data);
inc(b->right);
}
}
void inb(sn *b)
{
if(b)
{
inb(b->left);
inb(b->right);
printf("%c",b->data);
}
}
int leaf(sn *b)
{
if(b==NULL)
return 0;
if(b->left==NULL&&b->right==NULL)
return 1;
else
return leaf(b->left)+leaf(b->right);
}
int height(sn *b)
{
if(b==NULL)
return 0;
else
return height(b->left)>height(b->right)?height(b->left)+1:height(b->right)+1;
}
main( )
{
sn *t,*q;
t=Create(q);
inc(t);
printf("\n");
inb(t);
printf("\n");
printf("%d %d",leaf(t),height(t));
printf("\n");
}
二叉树
最新推荐文章于 2022-03-12 20:36:06 发布