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 -1234567890
#define eps 1e-8

using namespace std;

char bef[32];
char mid[32];
char aft[32];
char tree[1024];
bool visit[32];
int len;
int p;

void build(char root, int t)
{
        int i, j;
        tree[t] = root;
        visit[root-‘A‘] = true;
        for(i = 0; i < len; i++) if(mid[i] == root) break;
        for(j = 0; j < len; j++) if(bef[j] == root) break;
        if(i-1 >= 0 && !visit[mid[i-1]-‘A‘])
        {
                char lson = bef[j+1];
                build(lson, 2*t);
        }
        if(i+1 < len && !visit[mid[i+1]-‘A‘])
        {
                char rson;
                for(int k = j+1; k < len; k++)
                {
                        if(!visit[bef[k]-‘A‘])
                        {
                                rson = bef[k];
                                break;
                        }
                }
                build(rson, 2*t+1);
        }
}

void after(char root, int t)
{
        if(tree[2*t] != ‘\0‘) after(tree[2*t], 2*t);
        if(tree[2*t+1] != ‘\0‘) after(tree[2*t+1], 2*t+1);
        aft[p++] = root;
}

int main()
{
        #ifdef BellWind
                freopen("2255.in", "r", stdin);
        #endif // BellWind

        while (~scanf("%s %s", bef, mid))
        {
                memset(visit, 0, sizeof(visit));
                memset(tree, 0, sizeof(tree));
                len = strlen(bef);
                build(bef[0], 1);
//                int cnt = 0;
//                for(int c = 1; cnt < len; c++)
//                {
//                        if(tree[c] == ‘\0‘) cout << ‘ ‘;
//                        else {cnt++; cout << tree[c];}
//                }
//                cout << endl;
                p = 0;
                after(bef[0], 1);
                for(int c = 0; c < len; c++) cout << aft[c];
                cout << endl;
        }

        return 0;
}

  

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

POJ 2255 Tree Recovery(树的遍历)的相关文章

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 二叉树恢复

一道和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 = 二叉树遍历

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 解题心得

原题: 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 1741 Tree(树的点分治)

poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出来.接着对于每一个子树再次运算.如果不用点分治的技巧,时间复杂度可能退化成\(O(n^2)\)(链).如果对于子树重新选根,找到树的重心,就一定可以保证时间复杂度在\(O(nlogn)\)内. 具体技巧是:首先选出树的重心,将重心视为根.接着计算出每个结点的深度,以此统计答案.由于子树中可能出现重复

POJ 3723 Tree(树链剖分)

POJ 3237 Tree 题目链接 就多一个取负操作,所以线段树结点就把最大和最小值存下来,每次取负的时候,最大和最小值取负后,交换即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 10005; const int INF = 0x3f3f3f3f; int dep[N],