数据结构实验之二叉树三:统计叶子数

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 <stdio.h>
#include <stdlib.h>
#include <string.h> struct node
{
char c;
struct node *lt, *rt;
}; char s[100];
int i,k; struct node *creat()
{
struct node *root;
if(s[i]==','){
i++;
root=NULL;
}
else{
root=(struct node *)malloc(sizeof(struct node));
root->c=s[i++];
root->lt=creat();
root->rt=creat();
}
return root;
} void num(struct node *root)
{
if(root){
if(!root->lt&&!root->rt){
k++;
}
num(root->lt);
num(root->rt);
}
} int main()
{
while(~scanf("%s",s))
{
i=0;k=0;
struct node *root;
root=creat();
num(root);
printf("%d\n",k);
}
return 0;
}

SDUT OJ 数据结构实验之二叉树三:统计叶子数的更多相关文章

  1. SDUT 3342 数据结构实验之二叉树三:统计叶子数

    数据结构实验之二叉树三:统计叶子数 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知二叉 ...

  2. SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  3. SDUT OJ 数据结构实验之二叉树七:叶子问题

    数据结构实验之二叉树七:叶子问题 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

  4. SDUT OJ 数据结构实验之二叉树六:哈夫曼编码

    数据结构实验之二叉树六:哈夫曼编码 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  5. SDUT OJ 数据结构实验之二叉树五:层序遍历

    数据结构实验之二叉树五:层序遍历 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

  6. SDUT OJ 数据结构实验之二叉树四:(先序中序)还原二叉树

    数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...

  7. SDUT OJ 数据结构实验之二叉树二:遍历二叉树

    数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  8. SDUT OJ 数据结构实验之二叉树一:树的同构

    数据结构实验之二叉树一:树的同构 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

  9. SDUT OJ 数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

随机推荐

  1. SpringMVC+springSecurity+flexPaper 配置--类似百度文库在线预览

    背景:现在项目需要做一个类似百度文库的在线预览功能,在网上找了下很多人推荐使用FlexPaper,所以今天尝试学习了FlexPaper顺便集成到现有的框架中 由于网上目前的说的都不是很详细,所以现在记 ...

  2. php常用 随机数

    <?php $number =range(1,50); //shuffle 将数组顺序随即打乱. shuffle($number); print_r($number); echo '<br ...

  3. 一个jquery在不同浏览器下的兼容性问题。

    <div id ='pdiv' style='visibility:hidden;'> <div id='cdiv'>子元素</div> </div> ...

  4. 小程序为什么脚本内不能使用window等对象

    小程序(应用号)内不能使用window等对象. 页面的脚本逻辑在是在JsCore中运行,JsCore是一个没有窗口对象的环境,所以不能再脚本中使用window,也无法在脚本中操作组件.

  5. linux下安装sz/rz命令

    参考 https://blog.csdn.net/kobejayandy/article/details/13291655

  6. WEBAPI使用过滤器对API接口进行验证

    用户登录控制器:[ActionFilter]自定义过滤器 用户信息:var userData = new JObject();                   userData.Add(" ...

  7. 通过event事件来控制红绿灯通行车辆

    事件的初始值为False,所以最开始就是红灯,先模拟红绿灯的规律,设定为每两秒变换一次灯,然后再模拟车辆通行,通过事件来将两者的事件结合起来, 当事件为False时,为红灯,车辆处于等待状态,一直wa ...

  8. 分享一个好用的功能强大的节点树jQuery插件

    http://www.treejs.cn/

  9. The Difference Between @Helpers and @Functions In WebMatrix

    from: http://www.mikesdotnetting.com/article/173/the-difference-between-helpers-and-functions-in-web ...

  10. css总结3:Flex 布局教程:Flex-demos(转)

    上一篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法. 你会看到,不管是什么布局,Flex往往都可以几行命令搞定. 我只列出代码,详细的语法解释请查阅<Flex布局教程:语法篇&g ...