SDUT OJ 数据结构实验之二叉树五:层序遍历
数据结构实验之二叉树五:层序遍历
Problem Description
已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立二叉树并求二叉树的层次遍历序列。
Input
Output
Sample Input
2
abd,,eg,,,cf,,,
xnl,,i,,u,,
Sample Output
abcdefg
xnuli
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char c;
struct node *lt, *rt;
};
char s[100];
int i;
struct node *creat()
{
struct node *root;
root=(struct node *)malloc(sizeof(struct node));
if(s[i]==',') {i++;
return NULL;
}
else{
root->c=s[i++];
root->lt=creat();
root->rt=creat();
}
return root;
}
void ceng(struct node *root)
{
int out=0, in=0;
struct node *p[100];
p[in++]=root;
while(out<in)
{
if(p[out]){
printf("%c",p[out]->c);
p[in++]=p[out]->lt;
p[in++]=p[out]->rt;
}
out++;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
i=0;
scanf("%s",s);
struct node *root;
root=creat();
ceng(root);
printf("\n");
}
return 0;
}
SDUT OJ 数据结构实验之二叉树五:层序遍历的更多相关文章
- SDUT OJ 数据结构实验之二叉树二:遍历二叉树
数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- SDUT 3344 数据结构实验之二叉树五:层序遍历
数据结构实验之二叉树五:层序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...
- SDUT 3341 数据结构实验之二叉树二:遍历二叉树
数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知二叉 ...
- SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...
- 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 ...
- SDUT OJ 数据结构实验之二叉树三:统计叶子数
数据结构实验之二叉树三:统计叶子数 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
随机推荐
- CSS DIV 居中
左右居中 margin-left: auto; margin-right: auto; 上下居中
- oracle误删数据的解决方法
之前不小心误删了一条数据,索性我还记得id,通过select * from 表名 as of timestamp to_timestamp('2017-6-23 9:10:00','yyyy-mm-d ...
- The centos disc was not found in any of your drives.Please insert the centos disc and press OK to retry
查看虚拟机设置中关于CDROM的选项,将CDROM的状态改为已连接,不要奇怪,勾选上之后再按下OK就好了
- mfs权威指南
1. 我在性能测试中间遇到些问题,因为我时间有限,所以希望大家一起来测试解决,群策群力.有什么问题请大家及时指出来,因为我也处在一个不断摸索的阶段. 2. mfs不多做介绍,具体细节请参考本版mfs实 ...
- 819. Most Common Word 统计高频词(暂未被禁止)
[抄题]: Given a paragraph and a list of banned words, return the most frequent word that is not in the ...
- OpenCV---resize
转自http://www.cnblogs.com/korbin/p/5612427.html 在图像处理过程中,有时需要把图像调整到同样大小,便于处理,这时需要用到图像resize() 原函数void ...
- Luogu 2597 [ZJOI2012]灾难
BZOJ 2815. 解法还是挺巧妙的. 放上写得很详细很好懂的题解链接 戳这里. 一个物种$x$如果要灭绝,那么沿着它的入边反向走走走,一定可以走到一个点$y$,如果这个点$y$的物种灭绝了,那么 ...
- vuex 数据绑定
操作文档: 安装vuex: cnpm install vuex --save 文档介绍: https://vuex.vuejs.org/guide/modules.html import Vu ...
- Monkey压力测试Android常见的错误类型及黑白名单的使用方法
Android常见的错误类型有两种 1.ANR类型 1)在5秒内没有响应输入的事件(例如,按键按下,屏幕触摸) 2)BroadcastReceiver在10秒内没有执行完毕 2.Crash类型 1)异 ...
- ASP.NET多页面传递数据,附框架源码
很多时候我们需要把数据传递到多个页面,比如表单提交可以指定提交数据到某个页面,那么关闭某个页面怎么把数据传递到上一个页面或者它的父页面. 在这里我附一段源码用于当前页面关闭指定某个页面刷新. 子页面方 ...