SDUT OJ 数据结构实验之二叉树二:遍历二叉树
数据结构实验之二叉树二:遍历二叉树
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 数据结构实验之二叉树二:遍历二叉树的更多相关文章
- 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 Descrip ...
- SDUT OJ 数据结构实验之排序二:交换排序
数据结构实验之排序二:交换排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT 2142 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
数据结构实验之图论二:基于邻接表的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Descript ...
- SDUT 3399 数据结构实验之排序二:交换排序
数据结构实验之排序二:交换排序 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 冒泡排序和快 ...
- SDUT 3374 数据结构实验之查找二:平衡二叉树
数据结构实验之查找二:平衡二叉树 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 根据给定的输 ...
- 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 Probl ...
- SDUT OJ 数据结构实验之二叉树四:(先序中序)还原二叉树
数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...
随机推荐
- 利用jquery mobiscroll插件选择日期、select、treeList的具体运用
体验更优排版请移步原文:http://blog.kwin.wang/programming/jquery-mobiscroll-select-treeList.html mobiscroll是个很好用 ...
- BeX5 常见问题解决办法
1.获取当前Activity的名称 Activity activity = ProcessUtils.getActivityInProcessContext(); String activityNam ...
- love 玫瑰花
<!doctype html> <html> <head> <title>Love</title> <meta charset=&qu ...
- Make Cents
Every year, an elephant qualifies to the Arab Collegiate Programming Competition. He graduated this ...
- 供参考的 php 学习路线
供参考的 php 学习路线 第一阶段第一讲,WEB基础 1.1 网站基本知识: 1.2 网络协议介绍: 1.3 B/S与C/S结构的区别: 1.4 WEB编程.网站开发技术介绍. ...
- (三)maven出错记录
此处因为是自己做的所以文笔带过,简单描述下 1\首先是jsp页面报错,缺少必要的servletx依赖jar包,需要在pom中配置 <dependency><groupId>ja ...
- ubuntu 12.04 ubuntu System program problem detected 解决方法
1. ubuntu System program problem detected本人操作系统是ubuntu12.04,不知道是系统出了问题还是装的软件有问题,每次开机都出现:System progr ...
- HTML中禁用表中控件的两种方法与区别
在网页的制作过程中,我们会经常使用到表单.但是有时候我们会希望表单上的控件是不可修改的,比如在修改密码的网页中,显示用户名的文本框就应该是不可修改状态的. 在html中有两种禁用的方法,他们分别是: ...
- SpringBoot27 JDK动态代理详解、获取指定的类类型、动态注册Bean、接口调用框架
1 JDK动态代理详解 静态代理.JDK动态代理.Cglib动态代理的简单实现方式和区别请参见我的另外一篇博文. 1.1 JDK代理的基本步骤 >通过实现InvocationHandler接口来 ...
- opennebula模板对照比较
良好模板 有问题模板