• 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
    

    Source

     
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<sstream>
    #include<algorithm>
    #include<queue>
    #include<deque>
    #include<iomanip>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<stack>
    #include<set>
    #include<functional>
    #include<fstream>
    #include<memory>
    #include<list>
    #include<string>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    
    #define N 31
    #define INF 1000000009
    #define eps 0.00000001
    #define sf(a) scanf("%d",&a)
    const int MAXN = 1e5 + 3;
    
    char pre[N], mid[N];
    int L[MAXN], R[MAXN];
    int build(int l1, int r1, int l2, int r2)//l1 r1 是在前序序列的范围 l2 r2 是中序的范围
    {
        if (l1 > r1 || l2 > r2)
            return -1;
        if (l1 == r1&&l2 == r2)
            return l1;
        char tr = pre[l1];
        int p = l2;
        for (; p <= r2; p++)
        {
            if (mid[p] == tr)
                break;
        }
        L[l1] = build(l1 + 1, l1 + p - l2, l2, p - 1);
        R[l1] = build(l1 + p - l2 + 1, r1, p + 1, r2);
        return l1;
    }
    void print(int p)
    {
        if (L[p] != -1)
            print(L[p]);
        if (R[p] != -1)
            print(R[p]);
        printf("%c", pre[p]);
    }
    int main()
    {
        while (scanf("%s%s", pre, mid) != EOF)
        {
            memset(L, -1, sizeof(L));
            memset(R, -1, sizeof(R));
            int l = strlen(pre);
            int root = build(0, l - 1, 0, l - 1);
            print(root);
            printf("
    ");
        }
    }
  • 相关阅读:
    【LabVIEW】多列列表框使用汇总
    【LabVIEW】数据类型 汇总
    U-BOOT移植 前准备
    linux 的 输入子系统 与 平台设备系统个人理解
    关于内核编译的理解
    关于 内核编译make menuconfig 不能使用的解决
    函数式接口的使用 (Function、Predicate、Supplier、Consumer)
    获取单列集合,双列集合,数组的Stream流对象以及简单操作
    多线程的创建、匿名内部类方式创建线程、定义、调度步骤
    异常类的使用
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7469760.html
Copyright © 2020-2023  润新知