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

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

已知二叉树的一个按先序遍历输入的字符序列,如abc,,de,g,,f,,, (其中,表示空结点)。请建立二叉树并按中序和后序的方式遍历该二叉树。

Input

连续输入多组数据,每组数据输入一个长度小于50个字符的字符串。

Output

每组输入数据对应输出2行:
第1行输出中序遍历序列;
第2行输出后序遍历序列。

Sample Input

abc,,de,g,,f,,,

Sample Output

cbegdfa
cgefdba
#include <stdio.h>
#include <stdlib.h> struct node
{
char c;
struct node *lt, *rt;
}; char s[100];
int flag; struct node *creat()
{
struct node *root;
if(s[flag]==',') {
root=NULL;
flag++;
}
else{
root=(struct node *)malloc(sizeof(struct node));
root->c=s[flag++];
root->lt=creat();
root->rt=creat();
}
return root;
} void mid(struct node *root)
{
if(root){
mid(root->lt);
printf("%c",root->c);
mid(root->rt);
} } void last(struct node *root)
{
if(root){
last(root->lt);
last(root->rt);
printf("%c",root->c);
} } int main()
{
while(~scanf("%s",s)){
flag=0;
struct node *root;
root=creat();
mid(root);
printf("\n");
last(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 OJ 数据结构实验之串二:字符串匹配

    数据结构实验之串二:字符串匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  3. SDUT OJ 数据结构实验之排序二:交换排序

    数据结构实验之排序二:交换排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  4. SDUT 2142 数据结构实验之图论二:基于邻接表的广度优先搜索遍历

    数据结构实验之图论二:基于邻接表的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Descript ...

  5. SDUT 3399 数据结构实验之排序二:交换排序

    数据结构实验之排序二:交换排序 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 冒泡排序和快 ...

  6. SDUT 3374 数据结构实验之查找二:平衡二叉树

    数据结构实验之查找二:平衡二叉树 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 根据给定的输 ...

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

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

  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 ...

随机推荐

  1. 数据库与vs的连接

    新建一个MFC基于对话框的项目后,更改属性, 其中需要将include(里面都是MySQL的头文件)lib是库文件,将.dll放入与.exe同级目录下,或放入系统里(c:\windows\system ...

  2. php 实现百度文库搭建

    第一步:安装jodconverter,安装之后可以实现doc文档转成pdf. 文件下载地址为http://www.artofsolving.com/opensource/jodconverter 下载 ...

  3. WebFlux04 SpringBootWebFlux集成MongoDB之Windows版本、WebFlux实现CRUD、WebFlux实现JPA、参数校验

    1 下载并安装MongoDB 1.1 MongoDB官网 1.2 下载 solutions -> download center 1.3 安装 双击进入安装即可 1.3.1 安装时常见bug01 ...

  4. web 应用中访问 Spring 具体实现

    user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.Ora ...

  5. Apache htpasswd命令

    一.简介 htpasswd是apache的一个工具,该工具主要用于建立和更新存储用户名.密码的文本文件,主要用于对基于http用户的认证. 二.语法 Usage: htpasswd [-cimBdps ...

  6. C# Code 非常好的学习博客

    https://ardalis.com/how-to-become-master-writing-c-code

  7. ios7适配--navgationbar遮住下面view的处理

    3down votefavorite   Have you guys stumbled up on this issue ? Basically in iOS 7 Navigation Control ...

  8. TensorFlow安装教程

    Windows7 安装TensorFlow(本人试了好多方法后的成果):https://www.cnblogs.com/bxyan/p/6869237.html Linux: sudo pip ins ...

  9. ORCHARD学习教程-介绍

    ORCHARD 是什么? Orchard 是由微软公司创建,基于 ASP.NET MVC 技术的免费开源内容管理系统: 可用于建设博客.新闻门户.企业门户.行业网站门户等各种网站 简单易用的后台界面 ...

  10. .net Stream篇(六)

    BufferedStream 目录: 简单介绍一下BufferedStream 如何理解缓冲区? BufferedStream的优势 从BufferedStream 中学习装饰模式 如何理解装饰模式 ...