POJ 2255 Tree Recoveryw(二叉树)

题目原网址:http://poj.org/problem?id=2255

题目中文翻译:

Description

小瓦伦丁非常喜欢玩二叉树。 她最喜欢的游戏是用大写字母构造的随机二叉树。

这是她的一个创作的例子:

D
                                             
/ \
                                            
/   \
                                           
B     E
                                          
/ \     \
                                          /   \    
\
                                        
A     C     G
                                                   
/
                                                  
/
                                                  F

为了为后代记录她的树,她为每棵树写了两个字符串:前序遍历(根,左子树,右子树)和中序遍历(左子树,根,右子树)。 对于上面绘制的树,前序遍历是DBACEGF,中序遍历是ABCDEFG。

她认为这样一对字符串会提供足够的信息来重建树(但她从未尝试过)。

现在,多年以后,再次看到这些字符串,她意识到重建树确实是可能的,因为她从未在同一棵树上使用过两次相同的字母。

然而,手工重建很快就变得单调乏味。

所以现在她要求你写一个为她工作的程序!

Input

输入将包含一个或多个测试用例。

每个测试用例由一行包含两个字符串preord和inord,表示二叉树的前序遍历和中序遍历。 两个字符串都由不重复的大写字母组成。 (因此它们不超过26个字符。)

输入由文件结束(EOF)终止。

Output

对于每个测试用例,恢复瓦伦丁的二叉树并打印一行树的后序遍历(左子树,右子树,根)。

Sample Input

DBACEGF ABCDEFG

BCAD CBAD

Sample Output

ACBFGED

CDAB

解题思路:

通过先序遍历找到根,再根据中序遍历性质(一个节点的左儿子一定在它前面出现,而右儿子一定在它后面),找出这个节点的左右儿子,建树,最后输出后序遍历.

AC代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<string>
 4 #include<algorithm>
 5 #include<cstring>
 6
 7 using namespace std;
 8
 9 char q[30],z[30];
10 int xb;
11
12 void work(int l,int r) {
13     char a;
14     int i;
15     if(l > r) return ;
16     a = q[xb++];
17     for(i = l;i <= r; i++)
18         if(a == z[i])
19             break;
20     work(l,i - 1);
21     work(i + 1,r);
22     cout << a;
23 }
24
25 int main()
26 {
27     while(scanf("%s%s",q,z) != EOF) {
28         getchar();
29         int len = strlen(z);
30         xb = 0;
31         work(0,len-1);
32         cout << endl;
33     }
34
35     return 0;
36 }

原文地址:https://www.cnblogs.com/lipeiyi520/p/10739498.html

时间: 2024-08-09 20:55:22

POJ 2255 Tree Recoveryw(二叉树)的相关文章

POJ 2255 Tree Recovery 二叉树恢复

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

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

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

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

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 le

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 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 3437 Tree Grafting(有序树转化为二叉树)

Tree Grafting Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1834   Accepted: 795 Description Trees have many applications in computer science. Perhaps the most commonly used trees are rooted binary trees, but there are other types of r