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

http://poj.org/problem?id=2255

题意:给定先序遍历和中序遍历,求后序遍历。

回忆以前上DataStructure课的时候貌似写过类似的。

先从先序入手,从左到右扫描,进入时节点时立刻入栈,离开节点时立刻出栈。

关键是怎么知道什么时候才是立刻节点了呢?

貌似只有n^2的做法,因为要从中序遍历序列中找根。

但其实假如预处理出中序遍历的序列中的字母每个指向的位置就不用这额外的时间花费n了。

也就是从先序遍历入手,进入节点时把根节点的中序遍历序列指针两侧递归下去。

所以构造的形式如同一棵线段树。

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;

char s[1005];
char t[1005];
int ss[1005];
int tt[1005];
char st[1005];
int top;

void build(int sl, int sr, int tl, int tr) {
    if(tl != tr) {
        int tm = tt[s[sl]];
        if(tm > tl) {
            //左子树还有
            build(sl + 1, sl + tm - tl, tl, tm - 1);
        }
        if(tm < tr) {
            //右子树还有
            build(sl + tm - tl + 1, sl + sr, tm + 1, tr);
        }
    }
    st[++top] = s[sl];
}

int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
#endif // Yinku
    while(~scanf("%s%s", s + 1, t + 1)) {
        top = 0;
        int n = strlen(s + 1);
        for(int i = 1; i <= n; ++i) {
            ss[s[i]] = i;
            tt[t[i]] = i;
        }
        build(1, n, 1, n);
        for(int i = 1; i <= n; ++i)
            printf("%c", st[i]);
        printf("\n");
    }
}

实现起来的细节还是蛮多的。但本质上是通过dfs序连续这个性质进行分治。

原文地址:https://www.cnblogs.com/Inko/p/11719033.html

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

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 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 分治

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(树的遍历)

给定前序遍历和中序遍历,写出后序遍历. #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 树的遍历,分治 难度: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 Recoveryw(二叉树)

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

UVa536 Tree Recovery (二叉树遍历)

链接:http://acm.hust.edu.cn/vjudge/problem/19645分析:三种树的遍历方式中除了中序外再知道另外遍历方式得到的序列就可以唯一确定一棵二叉树.先序:先打印根再遍历左右子树.中序:先遍历左子树,然后打印根,再遍历右子树.后序:先遍历左右子树,最后打印根. 1 #include <cstdio> 2 #include <cstring> 3 4 char pre_order[30], in_order[30]; 5 6 void dfs(int L

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 / /