SDUT OJ 数据结构实验之二叉树三:统计叶子数
数据结构实验之二叉树三:统计叶子数
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 数据结构实验之二叉树三:统计叶子数的更多相关文章
- SDUT 3342 数据结构实验之二叉树三:统计叶子数
数据结构实验之二叉树三:统计叶子数 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知二叉 ...
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- SDUT OJ 数据结构实验之二叉树七:叶子问题
数据结构实验之二叉树七:叶子问题 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...
- SDUT OJ 数据结构实验之二叉树六:哈夫曼编码
数据结构实验之二叉树六:哈夫曼编码 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- SDUT OJ 数据结构实验之二叉树五:层序遍历
数据结构实验之二叉树五:层序遍历 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...
- SDUT OJ 数据结构实验之二叉树四:(先序中序)还原二叉树
数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...
- SDUT OJ 数据结构实验之二叉树二:遍历二叉树
数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- SDUT OJ 数据结构实验之二叉树一:树的同构
数据结构实验之二叉树一:树的同构 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...
- SDUT OJ 数据结构实验之串三:KMP应用
数据结构实验之串三:KMP应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
随机推荐
- QQ、邮箱、手机号 正则验证
邮箱:/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/ 手机号:/^(((13[0-9]{1})|(15[0-9]{1 ...
- Luogu 3646 [APIO2015]巴厘岛的雕塑
初赛成绩出了,和预想的一样,一分都没挂,开心. 大佬的博客 subtask 1 ($n \leq 200$) 因为高位取$0$一定比高位取$1$优,我们考虑按照位从高到低进行检验,设$f_{i, j} ...
- Django cache
Django中使用redis 方式一: utils文件夹下,建立redis_pool.py import redis POOL = redis.ConnectionPool(host='127.0.0 ...
- Gson 配置解析
之前项目用到了gson对json和Java类之间互转,现在将gson的配置总结一下. 首先,创建gson对象之间,建立gsonbuilder对象,并配置 // 不导出实体类中没有用@Expose注解的 ...
- 无限级分类及生成json数据
第一步,先去数据库查询类别数据,然后交给生成json数据的函数处理,代码如下: /*生成类别JSON数据*/ public function wirteJson(){ $dataInfo = \thi ...
- Position Independent Code (PIC) in shared libraries
E原文地址:http://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries/下一文: ...
- Ubuntu普通用户使用串口设备
将普通用户加入dialout组,然后重启或注销登录 sudo gpasswd --add username dialout
- Mysql避免重复插入记录方法
一.mysql replace用法 1.replace into replace into table (id,name) values('1','aa'),('2','bb') 此语句的作用是向 ...
- 自建脚手架之配置中心--LightConf的实现
常规项目开发过程中, 通常会将配置信息位于在项目resource目录下的properties文件文件中, 配置信息通常包括有: jdbc地址配置.redis地址配置.活动开关--等等.因此每次上线或者 ...
- kubernetes 1.3管中窥豹- RS(Replica Sets):the next-generation Replication Controller
前言 kubernates 1.3出了几个新的概念,其中包括deployments,Replica Sets,并且官网称之为是the next-generation Replication Contr ...