二叉树中求叶子节点个数:
当二叉树不为空,而且左子树和右字数 不为空时:
#include
#include
typedef char ElemType;
typedef struct BiTNode{
ElemType data;
struct BiTNode* Lchild;
struct BiTNode* Rchild;
}BiTNode,*BiTree;
//构造二叉树
void createbt(BiTree &T)
{
char ch;
scanf("%c",&ch);
if(ch=='#')
T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiTNode));
T->data=ch;
createbt(T->Lchild);
createbt(T->Rchild);
}
}
//统计二叉树中叶子节点的个数
void Numnode(BiTree T,int &num)
{
if(T)
{
if((!T->Lchild)&&(!T->Rchild))
num++;
Numnode(T->Lchild,num);
Numnode(T->Rchild,num);
}
}
int main()
{
int count=0;
BiTree T;
printf("create a tree such as ABC##DE#G##F###
");
createbt(T);
printf("统计二叉树中叶子节点的个数
");
Numnode(T,count);
printf("%d
",count);
return 0;
}