数据结构实验之二叉树三:统计叶子数
2019-08-03 16:52
134 查看
数据结构实验之二叉树三:统计叶子数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
已知二叉树的一个按先序遍历输入的字符序列,如abc,de,g,f, (其中,表示空结点)。请建立二叉树并求二叉树的叶子结点个数。
Input
连续输入多组数据,每组数据输入一个长度小于50个字符的字符串。
Output
输出二叉树的叶子结点个数。
Sample Input
abc,de,g,f,
Sample Output
3
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; typedef struct node { char data; struct node *l; struct node *r; }Tree; char s[55]; int i, sum; Tree *creat() { char ch = s[i++]; Tree *tree; if(ch == ',') tree = NULL; else { tree = (Tree *)malloc(sizeof(Tree)); tree->data = ch; tree->l = creat(); tree->r = creat(); } return tree; } void left(Tree *tree) { if(tree == NULL) return ; else if(tree->l == NULL && tree->r == NULL)///左右子树为空,为叶子 sum++; left(tree->l);///遍历左右子树 left(tree->r); } int main() { while(~scanf("%s", s)) { i = 0; Tree *tree; tree = creat(); sum = 0; left(tree); printf("%d\n", sum); } return 0; }
相关文章推荐
- 数据结构实验之二叉树三:统计叶子数
- sdutacm-数据结构实验之二叉树三:统计叶子数
- 3342 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- OJ3342 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验之二叉树三:统计叶子数