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 ...
随机推荐
- MySQL备份还原之三使用xtrabackup
1 xtrabackup安装 1)解压源码包 tar -xzvf percona-xtrabackup-2.1.7.tar.gz 2)安装perl环境(DBI/DBD) yum install per ...
- uboot重定位代码分析(转)
概述 重定位(relocate)代码将BootLoader自身由Flash复制到SDRAM,以便跳转到SDRAM执行.之所以需要进行重定位是因为在Flash中执行速度比较慢,而系统复位后总是从0x00 ...
- 【原】Coursera—Andrew Ng机器学习—Week 2 习题—Linear Regression with Multiple Variables 多变量线性回归
Gradient Descent for Multiple Variables [1]多变量线性模型 代价函数 Answer:AB [2]Feature Scaling 特征缩放 Answer:D ...
- JS中日期处理
- [luogu3369]普通平衡树(treap模板)
解题关键:treap模板保存. #include<cstdio> #include<cstring> #include<algorithm> #include< ...
- C#连接Mysql数据库 MysqlHelper.cs文件
mysql.data.dll下载_c#连接mysql必要插件mysql.data.dll是C#操作MYSQL的驱动文件,是c#连接mysql必要插件,使c#语言更简洁的操作mysql数据库.当你的电脑 ...
- CMake代码示例
CMake代码示例(注:此文只贴了部分示例代码,详细文章见最后参考文章): 1.为工程和可执行文件指定一个版本号. 虽然可以在源代码中唯一指定它,但是在CMakeLists文件中指定它可以提供更好的灵 ...
- 局部变量和static变量的区别
static int a ; int b; scanf_s("%d %d",&a,&b); 01374212 lea eax,[b] 01374215 push e ...
- numpy ndarray 返回 index 问题
经常遇到需要返回满足条件的index. python中没有which函数,但有列表推导式, 可以实现类似功能 y= np.array([3,2,5,20]) yOut[131]: array([ 3, ...
- Luogu 1580 [NOIP2016] 换教室
先用Floyed做亮点之间的最短路,设计dp,记dp[i][j][0]为到第i节课,换了j次课,当前有没有换课达到的期望耗费体力最小值 方程(太长了还是看代码吧):dp[i][j][0]<-dp ...