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

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

PS:叶子就是左右儿子都为空的节点

#include <stdio.h>
#include <stdlib.h>
#include <string.h> typedef struct tree
{
char data;
struct tree *l,*r;
}tree; int num,i;
char s[55]; tree *newtree()//开辟新节点
{
tree *t;
t = (tree*)malloc(sizeof(tree));
t->l = t->r = NULL;
return t;
} tree *creat()//根据所给的先序遍历建立二叉树
{
tree *t = newtree();
if(s[i++]==',')
return NULL;
t->data = s[i-1];
t->l = creat();
t->r = creat();
return t;
} void get_num(tree *t)//统计叶子数
{
if(t)
{
if(t->l==NULL&&t->r==NULL)
num++;
get_num(t->l);
get_num(t->r);
}
} int main()
{
tree *t;
while(scanf("%s",s)!=EOF)
{
i = num = 0;
t = newtree();
t = creat();
get_num(t);
printf("%d\n",num);
}
return 0;
}

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

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

    数据结构实验之二叉树三:统计叶子数 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

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

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

  3. SDUT 3375 数据结构实验之查找三:树的种类统计

    数据结构实验之查找三:树的种类统计 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 随着卫星成 ...

  4. SDUT 3311 数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 有n个小朋友 ...

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

    数据结构实验之二叉树七:叶子问题 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...

  6. SDUT 3347 数据结构实验之数组三:快速转置

    数据结构实验之数组三:快速转置 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 转置运算是一 ...

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

    数据结构实验之二叉树六:哈夫曼编码 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 字符的编 ...

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

    数据结构实验之二叉树一:树的同构 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定两棵树 ...

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

    数据结构实验之二叉树五:层序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...

随机推荐

  1. VS2013 IIS Express8.0

    1.下载最新版本的 Microsoft Web Platform Installer 5.0. 2.在组件列表中选择最新版本的 WebMatrix 3.0,安装重启后即可正常使用 IIS Expres ...

  2. Spring MVC中获取当前项目的路径

    Spring MVC中获取当前项目的路径 在web.xml中加入以下内容 <!--获取项目路径--> <context-param> <param-name>web ...

  3. 浏览器在IE8 以下时显示提示信息,提示用户升级浏览器

    <!--[if lt IE 8]> <div style="background: #eeeeee;border-bottom: 1px solid #cccccc;col ...

  4. Ubuntu18.04 磁盘挂载在某目录下

    简介 记录Ubuntu18.04 桌面版系统下实现某个磁盘挂载到自己想要的目录下,内容参考网上教程,此处为自己操作记录. 查看当前所有的磁盘信息 命令:sudo fdisk -l 从列出的信息中可以看 ...

  5. flask的基本操作

    常用的SQLAlchemy字段类型 # coding:utf-8 from flask import Flask from flask_sqlalchemy import SQLAlchemy app ...

  6. Docker Mysql部署

    1.下载tomcat镜像 docker pull mysql 2.启动容器 docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD ...

  7. Linux巨好用的

    受不了了windows下配置烦死人.linux一站式搞定,无敌 安装python3 机子上py2 py3共存没问题 安装virtualenv (py3)分割实验环境非常稳 安装mysql 先安装了一 ...

  8. python 数据文件操作——读入数据

  9. ZOJ3195 Design the city [2017年6月计划 树上问题04]

    Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...

  10. 开始使用Apache弗林克和Mapr Streams

    Introduction MapR Ecosystem Package 2.0 (MEP) is coming with some new features related to MapR Strea ...