您的位置:首页 > 理论基础 > 数据结构算法

【数据结构基础】求二叉树叶子结点

2017-04-30 13:45 211 查看
#include<stdio.h>
#include<stdlib.h>
static  int count=0;
typedef struct TreeNode{
char element;
struct TreeNode *left,*right;
}Tree,*BTree;
BTree BuildTree(void){
BTree T;
char ch;
ch=getchar();
if(ch=='#'){
T=NULL;
}else{
T=(BTree)malloc(sizeof(Tree));
T->element=ch;
T->left=BuildTree();
T->right=BuildTree();
}
return T;
}
int CountLeaf(BTree T){
if(T){
if(!T->left && !T->right){
printf("%c",T->element);
count++;
}
CountLeaf(T->left);
CountLeaf(T->right);
}
}
int main(void){
BTree T;
int num;
T=BuildTree();
CountLeaf(T);
printf("\n%d\n",count);
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息