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 ...
随机推荐
- Delphi.NET
Delphi.NET Borland.VclDSnap.dll Borland.Vcl.TCustomClientDataSet TFieldType.ftBCD Borland.Vcl.TField ...
- 虚拟机之 LAMP
LAMP 就是Linux apache mysql php 一.下载: 安装下载工具 yum install wget -y mysql:5.5.47 wget http://mirrors.sohu ...
- [ShaderStaff] Sprite Outline外轮廓效果
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Unity5.3.8f1 Unity提供了2D Object Sprite对象,但是没有提供外轮廓Outline效果的支持 ...
- subprocess模块和logging模块
主要内容: 一.subprocess模块 二.logging模块 1️⃣ subprocess模块 三种执行命令的方法 subprocess.run(*popenargs, input=None, ...
- Unity3D Asset 导入&导出
[Unity3D Asset 导入&导出] 通过Assets->Export Package..菜单可以导出当前选中Assets.若没有选中Assets,则会导出全部assets. 在弹 ...
- EF删除,查询,Linq查询,Lambda查询,修改链接字符串
(1)//删除操作 public bool delete() { try { a_context = new AEntities(); b1 = new Table_1(); //删除只需要写主键就行 ...
- Quick Find
--------------------siwuxie095 Quick Find 这里介绍并查集的一种实现思路:Qui ...
- ubuntu16.04 安装openpose
安装 Anaconda3 Tensorflow-cpu python3tensorflow 1.4.1+opencv3, protobuf, python3-tk ================== ...
- MySQL5.7插入中文乱码
参考: https://blog.csdn.net/kelay06/article/details/60870138 https://blog.csdn.net/itmr_liu/article/de ...
- C#连接Mysql数据库 MysqlHelper.cs文件
mysql.data.dll下载_c#连接mysql必要插件mysql.data.dll是C#操作MYSQL的驱动文件,是c#连接mysql必要插件,使c#语言更简洁的操作mysql数据库.当你的电脑 ...