Poj 2255 Tree Recovery

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 ABCDEFG
BCAD CBAD

Sample Output

ACBFGED
CDAB
题意:已知先序和中序,求后序;;;
参考代码:https://blog.csdn.net/liuke19950717/article/details/51291799

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1005;
char a[maxn],b[maxn];
int x[maxn];//辅助数组
void dfs(int la,int ra,int lb,int rb)
{
    int i=x[a[la]-'A'];//定位节点的位置 
    int j=i-lb;//当前节点的左子树的字符长度 
    int k=rb-i;//当前节点的右子树的字符长度 
    if(j)
    {
        dfs(la+1,la+j,lb,i-1);//递归左子树
    }
    if(k)
    {
        dfs(la+j+1,ra,i+1,rb);//递归右子树
    }
    printf("%c",a[la]);//输出的次序就是后续遍历的结果 
}
int main()
{
    while(~scanf("%s%s",a,b))
    {
        int len=strlen(a);
        for(int i=0;i<len;++i)
        {
            x[b[i]-'A']=i;
            //数组里保存中序的每个字母的下标 
        }
        dfs(0,len-1,0,len-1);
        printf("
");
    }
    return 0;
}

模仿大佬:

 1 #include<iostream>
 2 #include<string>
 3 #include<map>
 4 #include<algorithm>
 5 using namespace std;
 6 map<char, int > mp;
 7 string stra, strb;
 8 void dfs(int a, int b, int c, int d)
 9 {
10     int i = mp[stra[a]];
11     int j = i - c;
12     int k = d - i;
13     if (j)
14     {
15         dfs(a + 1, a + j, c, i - 1);
16     }
17     if (k)
18     {
19         dfs(a + j + 1, b, i + 1, d);
20     }
21     cout << stra[a];
22 }
23 int main()
24 {
25     while (cin >> stra >> strb)
26     {
27         int len = stra.length();
28         mp.clear();
29         for (int i = 0; i < len; i++)
30             mp[strb[i]] = i;
31         dfs(0, len - 1, 0, len - 1);
32         cout << endl;
33     }
34     return 0;
35 }

//忧伤,原来二叉树的遍历还可以这么用,弄了两天的时间来弄懂别人代码的意思,,痛苦,不过收获也是蛮大的

原文地址:https://www.cnblogs.com/kangdong/p/9021320.html