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

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立二叉树并求二叉树的层次遍历序列。

Input

 输入数据有多行,第一行是一个整数t (t<1000),代表有t行测试数据。每行是一个长度小于50个字符的字符串。

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 数据结构实验之二叉树五:层序遍历的更多相关文章

  1. SDUT OJ 数据结构实验之二叉树二:遍历二叉树

    数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

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

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

  3. SDUT 3341 数据结构实验之二叉树二:遍历二叉树

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

  4. SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)

    数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...

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

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

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

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

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

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

  8. SDUT OJ 数据结构实验之二叉树四:(先序中序)还原二叉树

    数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...

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

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

随机推荐

  1. delphi 原生 ADODB.recordset

    ADODB.recordset ..\source\rtl\win\Winapi.ADOInt.pas..\17.0\OCX\Servers\ADODB2010.pasCLASS_Recordset: ...

  2. linux上的第一个c语言程序

    1.编辑源文件 输入命令如下: root@ubuntu:/home/amy# vi hello.c 文件内容如下: #include<stdio.h> int main() { print ...

  3. Cfree clion windows c语言 socket 网络编程

    server.c #include <stdio.h> #include <winsock2.h> #define SERVER_PORT 5208 //侦听端口 int ma ...

  4. Windows 环境下于虚拟环境中源码安装 cx_oracle

    安装前提条件: (1).安装 instantclient-basic-nt (2).安装 instantclient-sdk-nt (3).安装 Microsoft Visual C++ Compil ...

  5. laravel策略类,实现当前登陆的用户是否具有删除,修改文章的权限

    策略类依赖月门脸类Auth 首先创建一个门脸类 make:auth 然后再创建一个策略  php artisan make:policy PostPolicy 定义Auth的登陆类,用的是哪个模型登陆 ...

  6. memcache 加载(对象)所遇到的问题。资源

    <?php $mem =new memcache(); if($mem->connect('127.0.0.1','11211')){ echo '连接OK'.'<br>'; ...

  7. wdcp挂载数据盘为WWW以及之后出现的各种问题解决方法

    昨天晚上突然有客户反映服务器访问不了,经检查后是因为磁盘满了! 购买阿里云的ECS选择linux系统的时候会赠送20G的系统盘,通常来讲自己用的话基本够了,但是我们作为网络公司,安装了一个WDCP后给 ...

  8. 怎样使用Mock Server

    转载自:http://www.cnblogs.com/111testing/p/6091460.html 怎样使用Mock Server   一,去这里https://github.com/dream ...

  9. c# 获取客户端ip、mac、机器名、操作系统、浏览器信息

    d using System; using System.Collections.Generic; using System.Linq; using System.Web; using System. ...

  10. #Pragma Pack与内存分配

    博客转载自:https://blog.csdn.net/mylinx/article/details/7007309 #pragma pack(n) 解释一: 每个特定平台上的编译器都有自己的默认“对 ...