NYOJ tree 先序中序求后序
来源:互联网 发布:淘宝切片上传有空隙 编辑:程序博客网 时间:2024/06/02 00:33
Tree
时间限制:1000 ms | 内存限制:65535 KB
难度:3
- 描述
- Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
This is an example of one of her creations:D / \ / \ B E / \ \ / \ \ A C G / / F
To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!- 输入
- The input will contain one or more test cases.
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
Input is terminated by end of file. - 输出
- For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).
- 样例输入
DBACEGF ABCDEFGBCAD CBAD
- 样例输出
ACBFGEDCDAB
我的AC代码
算法思想:
根据先序第一个必为根节点,根据中序找出根节点的左右子树,递归
建树,然后后序遍历此树!
#include <iostream>
02.
#include <cstring>
03.
using
namespace
std;
04.
struct
node
05.
{
06.
char
d;
07.
node* leftchild;
08.
node* rightchild;
09.
}r[50];
10.
node* create(
int
n,
char
a[],
char
b[],node* r)
11.
{
12.
int
i;
13.
if
(n == 0)
14.
return
NULL;
15.
for
(i = 0;i < n;i++)
16.
{
17.
if
(a[0] == b[i])
18.
{
19.
r->d = a[0];
20.
r->leftchild = create(i,a+1,b,r+1);
21.
r->rightchild = create(n-i-1,a+i+1,b+i+1,r+i+1);
22.
break
;
23.
}
24.
}
25.
return
r;
26.
}
27.
void
postorder(node* r)
28.
{
29.
if
(r == NULL)
30.
return
;
31.
postorder(r -> leftchild);
32.
postorder(r -> rightchild);
33.
cout <<
char
(r -> d);
34.
}
35.
int
main()
36.
{
37.
char
a[50],b[50];
38.
int
m;
39.
while
(cin >> a >> b)
40.
{
41.
m =
strlen
(a);
42.
create(m,a,b,r);
43.
postorder(r);
44.
cout << endl;
45.
}
46.
}
0 0
- NYOJ tree 先序中序求后序
- NYOJ--Tree
- nyoj Tree
- nyoj-221-Tree
- tree(nyoj)
- nyoj 221-Tree
- NYOJ 221 Tree
- NYOJ 题目221 Tree
- nyoj-221 Tree
- nyoj 221 Tree
- NYOJ-题目221 Tree
- nyoj 221 Tree 【已知前序中序求后序】
- nyoj 129 poj 1308 Is a tree
- NYOJ-221 Tree(前序中序推后序)
- NYOJ 221 Tree (二叉树遍历)
- NYOJ 679 The Weight of Tree 搜索+dp+邻接表
- HDU 1325 || NYOJ 129 || POJ 1308 Is It A Tree?
- NYOJ 211 tree(二叉树的遍历与创建)
- Apache+PHP+MySQL 开发环境搭建
- javase核心day13
- Asset资源的使用
- 格子操作
- Linux常用命令大全
- NYOJ tree 先序中序求后序
- winpcap学习笔记--(Opening an adapter and capturing the packets)
- vmtools的安装
- 黑马程序员---JAVA之多线程
- Comparable& Comparator
- 曾经的岁月-ASM离职信。
- 文本文件和二进制文件比较
- XPATH获取值的方法
- cf6.4A