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: 
                                               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. 

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