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;
    }
  • 相关阅读:
    MongoDB学习笔记~大叔分享批量添加—批量更新—批量删除
    Eclipse常用快捷键
    UDP 通信
    HDU 3571 N-dimensional Sphere(高斯消元 数论题)
    【机器学习】SVM核函数
    获取CentOS软件源中的updates包
    13年7月13日CF练习 Codeforces Round #147 (Div. 2)
    VMware vSphere 服务器虚拟化之二十八 桌面虚拟化之安装View传输服务器
    .net下灰度模式图像在创建Graphics时出现:无法从带有索引像素格式的图像创建graphics对象 问题的解决方案。
    庖丁图解八皇后问题
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/3239053.html
Copyright © 2011-2022 走看看