• 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;
    }
  • 相关阅读:
    iOS 第四期考核题(字符串/字典/数组的使用)
    oc之字典创建 复制 获取key value值
    oc之字典排序(将字符串转换成数字排序) 把字典放在数组内进行输出 字典赋值
    oc之可变字典创建 添加 删除 遍历
    oc之NSSortDescriptor(描述器排序)
    oc之获取系统当前时间的方法
    oc之数组排序 id nsobject instancetype的区别
    oc之类排序
    oc--习题
    oc 笔记--NSArray NSMutableArray 创建 添加 查询 复制 遍历等
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/3239053.html
Copyright © 2020-2023  润新知