关于数据结构里的算法问题!问题是:写出统计二叉树中叶子结点数的算法.有哪位会尽快解答,要详细答案!

来源:百度知道 编辑:UC知道 时间:2024/06/07 19:53:29

#include "stdio.h"
#include "malloc.h"
typedef int TElemType;
typedef struct BitNode;
{TElemType data;
struct BitNode *lchild,*rchild);
}BitNode,BitTree;
BitTree CreatBitTree(void)
{BitTree bt;TElemType x;
scanf("%d",&x);
if(x==-1) bt=NULL;
else
{bt=(BitTree)malloc(sizeof(BitNode));
bt->date=x;
bt->lchild=CreatBitTree();
bt->rchild=CreatBitTree();
}return bt;
}
int n=0;
void leafcount(BitTree bt)
{if(bt!=NULL)
{if(bt->lchild==NULL && bt->rchild==NULL)
n++;
leafcount(bt->lchild);
leafcount(bt->rchild);
}
}
void main(){
BitTree bt;
bt=CreatBitTree();
leafcount(bt);
printf("%d",n);
}