#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node{
char data ;
struct node * lchild;
struct node * rchild;
}BiTree;
BiTree * CreatTree();
int Count(BiTree * );
void Preorder(BiTree *);
int main(){
BiTree * top = NULL;
top = CreatTree();
printf("遍历结果: ");
Preorder(top);
putchar('\n');
printf("叶子节点的个数: %d\n",Count(top));
system("pause");
return 0;
}
BiTree * CreatTree(){
BiTree *t;
char ch ;
ch = getchar();
if (ch != '#'){
t = (BiTree *)malloc(sizeof(BiTree));
t ->data = ch ;
t->lchild = CreatTree();
t->rchild = CreatTree();
}
else{
t=NULL;
}
return t;
}
int Count(BiTree * top){
if(top == NULL){
return 0;
}
else if ((top->lchild==NULL) && (top->rchild==NULL)){
return 1;
}
else{
return Count(top->lchild)+Count(top->rchild);
}
}
void Preorder(BiTree * top ){
if(top != NULL){
printf("%c ",top->data);
Preorder(top->lchild);
Preorder(top->rchild);
}
}
相关文章
- 镜像二叉树和求二叉树最大深度(java)
- 【Super数据结构】二叉树的概念、操作大集合!(含深度/广度优先遍历/求深度/前序+中序构建二叉树/后序+中序构建二叉树等)-二叉树的概念和结构
- 数据结构(3) 第三天 栈的应用:就近匹配/中缀表达式转后缀表达式 、树/二叉树的概念、二叉树的递归与非递归遍历(DLR LDR LRD)、递归求叶子节点数目/二叉树高度/二叉树拷贝和释放
- 文本左右对齐(字符串、模拟)、螺旋矩阵 II(数组、矩阵)、二叉树中的最大路径和(树、深度优先搜索)
- Java实现二叉树的深度优先遍历和广度优先遍历算法示例
- HDU 1710 (二叉树的前序和中序,求后序)
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点
- 跪求C语言实现二叉树的深度和根
- 求满二叉树(哈夫曼树)叶子节点和非叶子节点的数目
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和