代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct bitnode
{
char data;
struct bitnode *lchild,*rchild;
}bitnode,*bitree;
bitree create(bitree t)
{
char ch;
scanf("%c",&ch);
if(ch=='#')
t=NULL;
else
{
if(!(t=(bitnode*)malloc(sizeof(bitnode))))
printf("overflow\n");
t->data=ch;
t->lchild=create(t->lchild);
t->rchild=create(t->rchild);
}
return t;
}
int num=0;
void countleaf(bitree t)
{
if(t)
{
if(!(t->lchild)&&!(t->rchild))
num++;
countleaf(t->lchild);
countleaf(t->rchild);
}
}
int main()
{
bitree t=NULL;
printf("请输入二叉树先序序列:");
t=create(t);
printf("叶子节点的个数为:\n");
countleaf(t);
printf("%d\n",num);
return 0;
}
图片: