传送门1: https://uva.onlinejudge.org/external/5/536.pdf

传送门2: http://acm.gdufe.edu.cn/Problem/read/id/1077

题意一样   输入不一样

HINT:

1. Preorder : (root, left subtree, right subtree);
  Inorder : (left subtree, root, right subtree);
  Postorder: (left subtree, right subtree, root);

2. 对于preorder,第一个元素即为整棵树的根
  且在preorder中,待求子树结点靠前为根结点

3. 以找到的点为界,将inorder划分为两棵子树

#include <bits/stdc++.h>
using namespace std; string pre, in;
int length;
char ans[]; void process(int l, int r){
if(l > r) return;
bool flag = false;
char ch;
int mid;
for(int i = ; i < pre.length(); ++i){
for(int j = l; j <= r; ++j){
if(pre[i] == in[j]){
flag = true;
ch = pre[i];
mid = j;
break;
}
}
if(flag) break;
}
ans[--length] = ch;   //注意先右后左(后序遍历从左到右再到根)
process(mid + , r);
process(l, mid - );
} int main(){
while(cin >> pre >> in){
length = pre.length();
process(, length - );
for(int i = ; i < pre.length(); ++i) cout << ans[i];
cout << endl;
}
return ;
}

另一种做法是建树模拟,贴上小光师兄博客里这道题的链接: http://blog.csdn.net/u012469987/article/details/41294313

UVa 536 Tree Recovery | GOJ 1077 Post-order (习题 6-3)的更多相关文章

  1. UVa 536 Tree Recovery(二叉树后序遍历)

    Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...

  2. UVa 536 Tree Recovery

    题意:给出一颗二叉树的前序遍历和中序遍历,输出其后序遍历 用杭电1710的代码改一点,就可以了. #include<iostream> #include<cstdio> #in ...

  3. UVA 536 Tree Recovery 建树+不建树

    题意: 给出先序和中序,求后序. 思路: ①建树然后递归输出. //建树的 #include<iostream> #include<cstdio> #include<qu ...

  4. UVA - 536 Tree Recovery (二叉树重建)

    题意:已知先序中序,输出后序. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio& ...

  5. 【UVA】536 Tree Recovery(树型结构基础)

    题目 题目     分析 莫名A了     代码 #include <bits/stdc++.h> using namespace std; string s1,s2; void buil ...

  6. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  7. UVA 536 (13.08.17)

     Tree Recovery  Little Valentine liked playing with binary trees very much. Her favoritegame was con ...

  8. Tree Recovery(前序中序求后序)

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14640   Accepted: 9091 De ...

  9. poj 2255 Tree Recovery 分治

    Tree Recovery Description Little Valentine liked playing with binary trees very much. Her favorite g ...

随机推荐

  1. 七、rdd究竟是什么

    RDD是个抽象类,定义了诸如map().reduce()等方法,但实际上继承RDD的派生类一般只要实现两个方法: def getPartitions: Array[Partition] def com ...

  2. flexbox应用举例

    我们常说的"flexbox"其实包含"父元素","子元素"2个部分,将"父元素"定义为一个flexbox,则在" ...

  3. 第一百二十九节,JavaScript,理解JavaScript库

    JavaScript,理解JavaScript库 学习要点: 1.项目介绍 2.理解JavaScript库 3.创建基础库 从本章,我们来用之前的基础知识来写一个项目,用以巩固之前所学.那么,每个项目 ...

  4. C语言:XML学习

    说明:文章中代码是我参考别人的例子得来的. XML基础 一.XML的概念 Xml是一种可扩展标记语言.在电子计算机中,标记指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种的信息比如 ...

  5. Eclipse/MyEclipse 安裝後應該更改的設置

    基本上都通過 Window -> Preferences 進行設置: Java 保存自動格式化: Java:Java -> Editor -> Save Actions,選中 Per ...

  6. C# EnumHelper

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection ...

  7. 在Windows上创建同样的Linux操作环境

    在之前的文章中,介绍了我在GNU/Linux图形界面环境下所使用的工具集合.其基本目的是在保证占用最少系统资源的条件下,将电脑操作效率推向极致.这样的工具组合尤如瑞士军刀一般,简洁.高效.功能全面.与 ...

  8. java四种xml解析区别

    1.DOM解析 dom解析是根据树形结构解析,将整个文档加载到内存中,因此对内存的要求较高,所以可以对该文档数据进行多次操作(增,删,改,查). 2.SAX解析 SAX解析是根据事件模型解析,边读取文 ...

  9. winform窗体对象 单例模式与泛型结合

    实现弹出窗体对象的单例模式  结合泛型后,可以用于所有窗体的弹出操作 public class BaseFrm<T> where T : Form, new() { //定义一个静态的,私 ...

  10. 多标记学习--Learning from Multi-Label Data

    传统分类问题,即多类分类问题是,假设每个示例仅具有单个标记,且所有样本的标签类别数|L|大于1,然而,在很多现实世界的应用中,往往存在单个示例同时具有多重标记的情况. 而在多分类问题中,每个样本所含标 ...