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;
    }
  • 相关阅读:
    【视频+图文】带你快速掌握Java中含continue语句的双重for循环
    【小白视频学Java for循环】3分钟学会Java的for循环,让看懂for循环嵌套再不是难事
    【机器学习基础】交叉熵(cross entropy)损失函数是凸函数吗?
    【tf.keras】tensorflow datasets,tfds
    【python3基础】命令行参数及 argparse
    【机器学习实战】验证集效果比测试集好怎么办?
    [主动学习--查询策略] 01 Core-set
    Monte-Carlo Dropout,蒙特卡罗 dropout
    NumPy 会自动检测并利用 GPU 吗?
    Linux 和 Windows 查看 CUDA 和 cuDNN 版本
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/3239053.html
Copyright © 2011-2022 走看看