poj 2255 Tree Recovery

Tree Recovery

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12413   Accepted: 7765

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

Source

Ulm Local 1997

 1 #include <stdio.h>
 2 #include <cstring>
 3 #include <queue>
 4 #include <iostream>
 5 using namespace std;
 6 void findmid(string pre,string in){
 7     //cout<<"1 "<<pre<<" "<<in<<endl;
 8     char c=pre[0];//根节点
 9     //cout<<"1"<<c<<endl;
10     int i;
11     for(i=0;i<in.length();i++){
12         if(c==in[i]){
13             break;
14         }
15     }
16     if(i)  //左子树不空
17     findmid(pre.substr(1,i),in.substr(0,i));
18     if(i<in.length()-1)//右子树不空
19     findmid(pre.substr(i+1),in.substr(i+1));
20     cout<<c;
21 }
22 int main(){
23     //freopen("D:\\INPUT.txt","r",stdin);
24     string pre,in;
25     while(cin>>pre>>in){
26             findmid(pre,in);
27             cout<<endl;
28     }
29     return 0;
30 }
时间: 2024-10-12 18:50:28

poj 2255 Tree Recovery的相关文章

POJ 2255 Tree Recovery 二叉树恢复

一道和Leetcode的一道题目基本上一样的题目. 给出前序遍历和中序遍历序列,要求根据这些信息恢复一颗二叉树的原貌,然后按后序遍历序列输出. Leetcode上有给出后序和中序,恢复二叉树的. 不过其实算法都是一样的.仿佛又回到了做Leetcode题的那段岁月中了. 还有就是输入是我特别处理过的,就两个函数,大家会了的无视,不会的可以学习下. #include <stdio.h> #include <string> #include <algorithm> using

poj 2255 Tree Recovery 分治

Tree Recovery 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

POJ 2255 Tree Recovery &amp;&amp; Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)

链接:poj.org/problem?id=2255 题意: 分别给你一个二叉树的前序遍历序列和中序遍历序列,让你给出这个二叉树的后序遍历序列. 思路: 对于二叉树的三种遍历方式,都可以使用递归来实现,那么也一定可以使用递归来拆解,以达到从遍历序列确定二叉树具体结构的目的.对于前序遍历来说,第一个字母一定是根,并且在序列中根的左子树包含的点一定出现在根的右子树的前面.对于中序遍历序列来说,根前面出现的字母所代表的点一定出现在左子树中,根后面出现的字母所代表的点一定出现在右子树中.在根据前序与中序

POJ 2255 Tree Recovery 树的遍历,分治 难度:0

http://poj.org/problem?id=2255 #include<cstdio> #include <cstring> using namespace std; const int maxn = 27; char pre[maxn],in[maxn]; char past[maxn]; void tre(int ps,int pe,int is,int ie,int& ind) { int lnum = strchr(in,pre[ps]) - in - is

POJ 2255 Tree Recovery(树的遍历)

给定前序遍历和中序遍历,写出后序遍历. #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <assert.h> #include <algorithm> #define MAX 1234567890 #define MIN

POJ - 2255 - Tree Recovery = 二叉树遍历

http://poj.org/problem?id=2255 题意:给定先序遍历和中序遍历,求后序遍历. 回忆以前上DataStructure课的时候貌似写过类似的. 先从先序入手,从左到右扫描,进入时节点时立刻入栈,离开节点时立刻出栈. 关键是怎么知道什么时候才是立刻节点了呢? 貌似只有n^2的做法,因为要从中序遍历序列中找根. 但其实假如预处理出中序遍历的序列中的字母每个指向的位置就不用这额外的时间花费n了. 也就是从先序遍历入手,进入节点时把根节点的中序遍历序列指针两侧递归下去. 所以构造

POJ 2255 Tree Recovery 解题心得

原题: 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 / /

POJ 2255 Tree Recoveryw(二叉树)

题目原网址:http://poj.org/problem?id=2255 题目中文翻译: Description 小瓦伦丁非常喜欢玩二叉树. 她最喜欢的游戏是用大写字母构造的随机二叉树. 这是她的一个创作的例子: D                                               / \                                              /   \                                         

Tree Recovery POJ - 2255

Tree Recovery POJ - 2255 根据树的前序遍历和中序遍历还原后序遍历. (偷懒用了stl的find) 1 #include<iostream> 2 #include<string> 3 using namespace std; 4 string s1,s2; 5 int len; 6 void work(int l1,int r1,int l2,int r2) 7 { 8 if(l1==r1) 9 { 10 cout<<s1[l1]; 11 retu