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

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. hadoop文件写入

    转:http://blog.csdn.net/xiaoshunzi111/article/details/48198105 由上图可知;写入文件分为三个角色,分别是clientnode  nameno ...

  2. KinderEditor编辑器 在Asp.Net中的使用

    KinderEditor编辑器的使用 分为简单的三步. 1:添加引用部分 <script src="/KinderEditor/kindeditor-min.js">& ...

  3. 【NOI2002】荒岛野人

    [题解] 可以枚举m 那么任意两个野人之间有 c[i]+x*p[i]=c[j]+x*p[j] (mod m)  无解,或 x 的最小值<=min(l[i] , l[j]) 化为丢番图方程:(p[ ...

  4. 使用Java创建XML数据

    ------------siwuxie095                         工程名:TestCreateXML 包名:com.siwuxie095.xml 类名:CreateXML. ...

  5. Opencv3 Mat对象构造函数与常用方法

    构造函数 Mat() Mat(int rows,int cols,int type) Mat(Size size,int type) Mat(int rows,int cols,int type,co ...

  6. MySQL5.7插入中文乱码

    参考: https://blog.csdn.net/kelay06/article/details/60870138 https://blog.csdn.net/itmr_liu/article/de ...

  7. Unix基本系统数据类型和stat结构体

    Unix基本系统数据类型 历史上,某些UNIX变量已与某些C数据类型联系在一起,例如,历史上主.次设备号存放在一个1 6位的短整型中, 8位表示主设备号,另外8位表示次设备号.但是,很多较大的系统需要 ...

  8. mvc json 日期问题的最简单解决方法

    1.首先编写BaseController这个类,需要引入Newtonsoft.Json.dll程序集 using System;using System.Collections.Generic;usi ...

  9. T-SQL分页功能存储过程

    分页功能存储过程 ALTER PROCEDURE [dbo].[P_SplitPagesQuery] @TablesName NVARCHAR(MAX),--表名或视图名(只能传单一表名) @PK N ...

  10. 编写高质量代码改善C#程序的157个建议——建议12: 重写Equals时也要重写GetHashCode

    建议12: 重写Equals时也要重写GetHashCode 除非考虑到自定义类型会被用作基于散列的集合的键值:否则,不建议重写Equals方法,因为这会带来一系列的问题. 如果编译上一个建议中的Pe ...