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

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立该二叉树并按从上到下从左到右的顺序输出该二叉树的所有叶子结点。

Input

输入数据有多行,每一行是一个长度小于50个字符的字符串。

Output

按从上到下从左到右的顺序输出二叉树的叶子结点。

Sample Input

abd,,eg,,,cf,,,

xnl,,i,,u,,

Sample Output

dfg

uli

题解:建立二叉树然后找叶子,注意叶子是从“上到下从左到右的顺序”输出,即层序遍历

#include <stdio.h>
#include <stdlib.h>
#include <string.h> typedef struct tree
{
char data;
struct tree *l,*r;
}tree; int 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)
{
tree *q[55],*t1;
int front,base;
front = base = 0;
if(t)
{
q[base++] = t;
}
while(front!=base)
{
t1 = q[front++];
if(t1->l==NULL&&t1->r==NULL)
{
printf("%c",t1->data);
continue;
}
if(t1->l)
q[base++] = t1->l;
if(t1->r)
q[base++] = t1->r;
}
} int main()
{
tree *t;
while(scanf("%s",s)!=EOF)
{
i = 0;
t = newtree();
t = creat();
get_num(t);
printf("\n");
}
return 0;
}

SDUT-3346_数据结构实验之二叉树七:叶子问题的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. c++设计模式:代理模式

    代理模式的实现和visitor实现有一曲同工之妙.这里就不多说了,也是实现set_delegate接口,以及回调函数处理接口. 主要区别在于: visitor主要是把信息回调之后就不管了. 代理模式主 ...

  2. TZ_05_Spring_事物的xml开发和annotation开发

    1.Spring_事物的xml开发 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...

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

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

  4. AutoMapper introduction

    http://automapper.org/ A convention-based object-object mapper映射. 100% organic and gluten-free. Take ...

  5. Spring_boot_pom.xml和启动方式

    spring-boot-starter-parent  整合第三方常用框架信息(各种依赖信息) spring-boot-starter-web   是Springboot整合SpringMvc Web ...

  6. 【Codeforces Round #430 (Div. 2) D】Vitya and Strange Lesson

    [链接]点击打开链接 [题意] 给出一个数组,每次操作将整个数组亦或一个数x,问得到的数组的结果中的mex.mex表示为自然数中第一个没有出现过的数. [题解] 异或的效果是可以累加的,所以不用每次都 ...

  7. 洛谷3128 [USACO15DEC]最大流Max Flow——树上差分

    题目:https://www.luogu.org/problemnew/show/P3128 树上差分.用离线lca,邻接表存好方便. #include<iostream> #includ ...

  8. android中的http框架,使其更加简单易用

    Afinal 是一个android的sqlite orm 和 ioc 框架. Afinal 是一个android的sqlite orm 和 ioc 框架.同时封装了android中的http框架,使其 ...

  9. hive行转列的高级用法later view explode

    先贴出一个示例: 参考链接

  10. typescript+react+antd基础环境搭建

    typescript+react+antd基础环境搭建(包含样式定制) tsconfig.json 配置 // 具体配置可以看上面的链接 这里module moduleResolution的配置都会影 ...