数据结构实验之二叉树二:遍历二叉树
来源:互联网 发布:淘宝食品类目有哪些 编辑:程序博客网 时间:2024/04/30 03:23
数据结构实验之二叉树二:遍历二叉树
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description
已知二叉树的一个按先序遍历输入的字符序列,如abc,,de,g,,f,,, (其中,表示空结点)。请建立二叉树并按中序和后序的方式遍历该二叉树。
Input
连续输入多组数据,每组数据输入一个长度小于50个字符的字符串。
Output
每组输入数据对应输出2行:
第1行输出中序遍历序列;
第2行输出后序遍历序列。
Example Input
abc,,de,g,,f,,,
Example Output
cbegdfacgefdba
Hint
Author
xam
01
#include <stdio.h>
02
#include <string.h>
03
#include <stdlib.h>
04
typedef
struct
node
05
{
06
char
data;
07
struct
node *l,*r;
08
}tree;
09
char
st[51];
10
int
i;
11
tree *creat(tree *root)
//这里是以先序遍历完成的树的保存
12
{
13
14
if
(st[++i]==
','
)
15
{
16
root=NULL;
17
}
18
else
19
{
20
root = (tree *)
malloc
(
sizeof
(tree));
21
root->data = st[i];
22
root->l = creat(root->l);
23
root->r = creat(root->r);
24
}
25
return
root;
26
}
27
void
zhongxu(tree *root)
//中序遍历
28
{
29
if
(root)
30
{
31
zhongxu(root->l);
32
printf
(
"%c"
,root->data);
33
zhongxu(root->r);
34
}
35
}
36
void
houxu(tree *root)
//后序遍历
37
{
38
if
(root)
39
{
40
houxu(root->l);
41
houxu(root->r);
42
printf
(
"%c"
,root->data);
43
}
44
}
45
int
main()
46
{
47
while
(~
scanf
(
"%s"
,st))
48
{
49
i = -1;
50
tree *root;
51
root = creat(root);
52
zhongxu(root);
53
printf
(
"\n"
);
54
houxu(root);
55
printf
(
"\n"
);
56
}
57
return
0;
58
}
0 0
- 数据结构实验之二叉树二:遍历二叉树
- 数据结构实验之二叉树二:遍历二叉树
- 数据结构实验之二叉树二:遍历二叉树
- SDUT3341数据结构实验之二叉树二:遍历二叉树
- 数据结构实验之二叉树二:遍历二叉树
- 数据结构实验之二叉树二:遍历二叉树
- 数据结构实验之二叉树二:遍历二叉树
- 数据结构实验之二叉树二:遍历二叉树
- 数据结构实验之二叉树二:遍历二叉树
- 数据结构实验之二叉树二:遍历二叉树
- 3341数据结构实验之二叉树二:遍历二叉树
- 数据结构实验之二叉树二:遍历二叉树
- 数据结构实验之二叉树二:遍历二叉树
- 数据结构实验之二叉树二:遍历二叉树
- 数据结构实验之二叉树二:遍历二叉树
- 数据结构实验之二叉树二:遍历二叉树
- 数据结构实验之二叉树二:遍历二叉树
- 数据结构实验之二叉树二:遍历二叉树
- Python中使用中文
- TCP为什么需要三次握手和四次挥手
- 【.Net码农】【MVC】多层架构+MVC+EF+AUTOFAC+AUTOMAPPER
- windows下设置tomcat内存
- setNeedsUpdateConstraints,needsUpdateConstraints等基于约束的AutoLayer的方法
- 数据结构实验之二叉树二:遍历二叉树
- ASP.NET中GridView使用简单攻略
- 变脸式应用 / 分页列表框架(四)
- 蓝桥杯 算法提高 P1001
- 北漂的程序猿何去何从
- UITableView如何开启极简模式
- Python正则表达式验证邮箱
- IBM HyperLedger fabric基础
- sql 2008获取表字段说明