数据结构实验之二叉树三:统计叶子数
2015-11-27 21:28
453 查看
#include <iostream> #include <cstdio> #include <cstdlib> using namespace std; int cnt; char s[101]; struct node { char data; struct node *rchild; struct node *lchild; }; struct node *buildtree() { struct node *root; if(s[cnt++]==',' )root=NULL; else { root=(struct node *)malloc(sizeof(struct node)); root->data=s[cnt-1]; root->rchild=buildtree(); root->lchild=buildtree(); } return root; }; int LeafCount (node *root) { if(root==NULL) return 0; else { if(root->lchild==NULL&&root->rchild==NULL) return 1; else return LeafCount(root->lchild)+LeafCount(root->rchild); } } int main() { while(cin>>s) { struct node *root; cnt=0; root=buildtree(); cout<<LeafCount(root)<<endl; } }
相关文章推荐
- 数据结构实验 第一单元 集合交并
- 数据结构实验 第一单元 集合交并
- 数据结构实验 第一单元 线性表之链表操作
- 数据结构实验 第一单元 线性表之链表操作
- 数据结构基础6.5:拓扑排序TopOrder
- 数据结构实验之图论八:欧拉回路
- 数据结构实验之图论七:驴友计划
- 数据结构基础6.4:最短路径(Dijkstra, Floyd)
- 数据结构上机【构造哈夫曼树,并输出对应节点的哈夫曼编码】
- 数据结构实验之图论六:村村通公路
- 数据结构上机 【创建二叉树,并采用先中后序遍历,输出树高,度数为分别为0 1 2 的结点个数】
- 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
- 数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
- getchar,getch,scanf,gets,getche一群纠结的兄弟
- [数据结构JAVA版]集合
- 数据结构之内存---对和栈的区别
- 数据结构实验之图论七:驴友计划 最短路中最小花费
- 贪婪算法+小应用(调度问题)
- 《大话数据结构》之Kruskal算法
- 数据结构-使用数组作为环