http://poj.org/problem?id=2255
来源:互联网 发布:淘宝客在哪里进去入口 编辑:程序博客网 时间:2024/05/22 13:05
Tree Recovery
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11380 Accepted: 7141
Description
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:
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!
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!
Input
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.
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.
Output
For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).
Sample Input
DBACEGF ABCDEFGBCAD CBAD
Sample Output
ACBFGEDCDAB
对于每个子树,先序遍历的第一个字母是子树的根,在中序遍历中这个根的左右分别是左子树和右子树。所以每次把子树的根压栈,在遍历子树的右子树和左子树。重复这个过程得到后序遍历。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <stack>using namespace std;stack<char>order;char preorder[30];char inorder[30];int len;void get_root(int l1, int r1, int l2, int r2) { int pos; if (l1 > r1) return; order.push(preorder[l1]); for (int i = l2; i <= r2; i ++) { if (order.top() == inorder[i]) { pos = i; break; } } get_root(l1+pos-l2+1, r1, pos+1, r2); get_root(l1+1, pos-l2+l1, l2, pos-1);}int main() { while (scanf("%s%s", preorder, inorder) != EOF) { len = strlen(preorder); get_root(0, len-1, 0, len-1); while (!order.empty()) { printf("%c", order.top()); order.pop(); } printf("\n"); }return 0;}
0 0
- http://poj.org/problem?id=2255
- http://poj.org/problem?id=2965
- http://poj.org/problem?id=2075
- http://poj.org/problem?id=1125
- http://poj.org/problem?id=2983
- http://poj.org/problem?id=2406
- http://poj.org/problem?id=1961
- http://poj.org/problem?id=1486
- http://poj.org/problem?id=1062
- http://poj.org/problem?id=2983
- http://poj.org/problem?id=3159
- http://poj.org/problem?id=1201
- http://poj.org/problem?id=3259
- http://poj.org/problem?id=3233
- http://poj.org/problem?id=1273
- http://poj.org/problem?id=3281
- http://poj.org/problem?id=1007
- http://poj.org/problem?id=1840
- Android开发之单例模式初探
- 湖大训练赛8 Identification Codes
- 活出自己的意义
- css中margin的一些易忽略的问题
- 疯狂html5讲义(二):HTML5简的常用元素与属性(二):html5新增的通用属性
- http://poj.org/problem?id=2255
- SSRS:之为用户“NT AUTHORITY\NETWORK SERVICE”授予的权限不足,无法执行此操作。 (rsAccessDenied)
- hdu 4864
- cocos2d-x 精灵遮罩
- 51单片机定时器初值的计算
- snipMate安装 错误
- Java 内部类浅析
- 杭电1102————最小生成树prim
- 主成分分析pca和白化