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 ...
随机推荐
- Delphi IOS 上架
http://docwiki.embarcadero.com/RADStudio/Seattle/en/IOS_Mobile_Application_Development http://docwik ...
- java基础之对象当做参数传进方法的堆栈内存解析
值类型当做参数传进方法: 引用类型对象当做参数传进方法: String字符串当做参数传进方法:
- 【bzoj2705】[SDOI2012]Longge的问题
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2507 Solved: 1531[Submit][ ...
- 【总结整理】Edraw Max亿图图示软件快捷键(转)
Edraw Max亿图图示软件快捷键大全,你想要的都在这了! 用过Edraw Max亿图图示软件的一定知道,亿图是一款功能十分强大的图形图表设计软件.无论是流程图.思维导图.组织结构图.甘特图.网 ...
- 基于size的优化
----------------------siwuxie095 基于 size 的优化 在 union( p , q ...
- mongo_2 $in 和 $all 区别
in 只需满足( )内的某一个值即可, 而$all 必须满足[ ]内的所有值, > db.table1.find({}); { "_id" : ObjectId(" ...
- 345. Reverse Vowels of a String翻转字符串中的元音字母
[抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...
- Linux查看内存,负载状态
Linux查看内存,负载状态 查看内存使用情况 www.ahlinux.com cat /proc/meminfo MemTotal: 16332644 kB MemFree: ...
- ubuntu 环境变量设置
一:用于当前终端:在当前终端中输入:export PATH=$PATH:<你的要加入的路径>不过上面的方法只适用于当前终端,一旦当前终端关闭或在另一个终端中,则无效.export NDK_ ...
- CentOS7虚拟机安装Linux教程及安装后所遇到的问题
1.VMware Workstation15下载. 官方链接:http://download3.vmware.com/software/wkst/file/VMware-workstation-ful ...