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 ...
随机推荐
- EOFException异常的处理
TOmcat启动后报:IOException while loading persisted sessions: Java.io.EOFException错误 - IOException while ...
- .find()和.index()的区别
今天在复习基本数据类型——字符串的时候,有一点想法,总结一下: 字符串的定义:字符串是一个有序的字符集合,用于存储和表示基本的文字信息,用‘,“,‘’‘括起来的称之为字符串. 字符串的操作有很多种,比 ...
- Logos
[Logos] Logos is a component of the Theos development suite that allows method hooking code to be wr ...
- 【bzoj1015】星球大战starwar
1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 5139 Solved: 2332[Submit ...
- JS获得css样式即获得元素的计算样式(《Javascript精粹修订版》书摘)
为HTML文档中的元素指定样式可以有3种方法:使用内嵌样式.在页面的head中对Style进行声明以及外部 CSS 文件.元素的视觉效果往往是由上述3种方式的结合或者其中某一种方式来确定的,但是内嵌样 ...
- hibernate mapping文件中 xmlns会导致linq to xml 查询不到对应的节点
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- SQL 批量插入有标识列的数据
代码: SET IDENTITY_INSERT 表名 ON SET IDENTITY_INSERT 表名 OFF
- [原创]MongoDB C++ 驱动部分问题解决方案(MongoDB C++ Driver)
本文为我长时间开发以及修改MongoDB C++ Driver时的一些问题和解决方案.目前本文所介绍的相关引擎也已经发布闭源版本,请自行下载 库版本以及相关位置:http://code.google. ...
- 升级Ubuntu 12.04下的gcc到4.7
我们知道C++11标准开始支持类内初始化(in-class initializer),Qt creator编译出现error,不支持这个特性,原因在于,Ubuntu12.04默认的是使用gcc4.6, ...
- 【JAVA】虚拟机指令集
[JAVA]虚拟机指令集 – – – 0x00 nop 什么都不做 0x01 aconst_null 将null推送至栈顶 0x02 iconst_m1 将int型-1推送至栈顶 0x03 icons ...