zoukankan      html  css  js  c++  java
  • poj2255

                                                                                                                   Tree Recovery
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9710   Accepted: 6105

    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


    转:
    #include <iostream>
    using namespace std;
    char pre[100];//先序遍历
    char in[100];//中序遍历
    char post[100];//后序遍历
    int len;//节点个数
    void solve(int p1,int p2,int m1,int m2)
    {
        if(p1>p2)
            return ;
        int i;
        for(i=m1;i<=m2;i++)
        {
            if(in[i]==pre[p1])
                break;
        }
        post[--len]=pre[p1];//根,放到后序遍历的最后面
        if(p1==p2)//叶子节点
            return ;
        solve(p1+i-m1+1,p2,i+1,m2);//递归处理右子树,得到右子树后序遍历
        solve(p1+1,p1+i-m1,m1,i-1);//处理左子树,得到左子树后序遍历
    }
    int main()
    {
        while (cin>>pre>>in)
        {
            memset(post,0,sizeof(post));
            len=strlen(pre);
            solve(0,len-1,0,len-1);
            cout<<post<<endl;
        }
        
        return 0;
    }
  • 相关阅读:
    [杭电_HDU] 2013
    动态调整线程数的python爬虫代码分享
    wampserver 配置的几个坑(雾
    wampserver apache 403无权限访问 You don't have permission to access /index.html on this server
    [爬坑日记] 安卓模拟器1903蓝屏 没开hyper-v
    [单片机] ESP8266 开机自动透传
    [操作系统] 死锁预防和死锁避免
    [linux] 手机Deploy linux 桌面中文乱码
    XHTML基础
    JDBC_c3p0连接池
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/3239053.html
Copyright © 2011-2022 走看看