# include <stdio.h>
# include <stdlib.h>
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 CountLeaf(BiTree T,int &count){
if(T){
if((!T->Lchild)&&(!T->Rchild))
count++;
CountLeaf(T->Lchild,count);
CountLeaf(T->Rchild,count);
}
}
void main(){
int count=0;
BiTree T;
printf("create a tree such as ABC##DE#G##F###\n");
createbt(T);
printf("统计二叉树中叶子节点的个数\n");
CountLeaf(T,count);
printf("%d\n",count);
}
# include <stdlib.h>
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 CountLeaf(BiTree T,int &count){
if(T){
if((!T->Lchild)&&(!T->Rchild))
count++;
CountLeaf(T->Lchild,count);
CountLeaf(T->Rchild,count);
}
}
void main(){
int count=0;
BiTree T;
printf("create a tree such as ABC##DE#G##F###\n");
createbt(T);
printf("统计二叉树中叶子节点的个数\n");
CountLeaf(T,count);
printf("%d\n",count);
}