POJ 2255 Tree Recovery 树的遍历 水题

来源:互联网 发布:js 弹出输入框 编辑:程序博客网 时间:2024/06/06 11:04
Tree RecoveryTime Limit: 1000MS      Memory Limit: 65536KTotal Submissions: 13969        Accepted: 8716DescriptionLittle 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                                                    /                                                   /                                                  FTo 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!InputThe 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.OutputFor each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).Sample InputDBACEGF ABCDEFGBCAD CBADSample OutputACBFGEDCDABSourceUlm Local 1997

前序遍历+中序遍历 求 后序遍历

#include<iostream>#include<stdlib.h>#include<stdio.h>#include<string>#include<vector>#include<deque>#include<queue>#include<algorithm>#include<set>#include<map>#include<stack>#include<time.h>#include<math.h>#include<list>#include<cstring>#include<fstream>//#include<memory.h>using namespace std;#define ll long long#define ull unsigned long long#define pii pair<int,int>#define INF 1000000007#define pll pair<ll,ll>#define pid pair<int,double>char pre[30];char in[30];void f(int sPre,int ePre,int sIn,int eIn){    if(sPre>ePre)        return;    char root=pre[sPre];//前序遍历第一个节点是树根    for(int i=sIn;i<=eIn;++i){//寻找树根在中序遍历中的位置        if(in[i]==root){            int lenLeft=i-sIn;            f(sPre+1,sPre+lenLeft,sIn,i-1);//递归输出左子树            int lenRight=eIn-i;            f(sPre+lenLeft+1,sPre+lenLeft+lenRight,i+1,eIn);//右            break;        }    }    putchar(root);//输出根节点}int main(){    //freopen("/home/lu/文档/r.txt","r",stdin);    //freopen("/home/lu/文档/w.txt","w",stdout);    while(~scanf("%s%s",pre,in)){        int len=strlen(pre);        f(0,len-1,0,len-1);        putchar('\n');    }    return 0;}
0 0
原创粉丝点击