传送门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. 数据意识崛起,从企业应用看BI软件的未来发展

    前阵子,和一群企业CIO聊天,希望从甲方角度看看对BI产品的看法.在问及一些成熟企业为何不上BI项目时,大家纷纷表示目前还处于观望状态. 提及BI,大家都觉得有些飘忽,和大数据一样,听着高大上,能真正 ...

  2. 中国IT武林大会暨中国首席技术官2016年度人物颁奖盛典概况

    在"大众创业.万众创新"的互联网时代,深入实施创新驱动发展战略,建设创新型国家,必须大力推动"互联网+科技"的发展.由中国首席技术官联盟.CCTV证券频道< ...

  3. eclipse集成配置JDK和Tomcat

    在eclipse中集成JDK和tomcat服务器方法很简单,我们可以在服务器上运行想要的东西.比如我们学习javaweb时就要用到. 工具/原料   eclipse,JDK,tomcat 方法/步骤 ...

  4. js操作符总结

    算数操作符加法操作符(+),减法操作符(-),除法操作符(/),乘法操作符(*)还可以把多种操作组合在一起:1+4*5避免产生歧义,可以用括号把不同的操作分隔开来:1+(4*5):(1+4)*5变量可 ...

  5. MATLAB编译器

    1. mcc filename.cpp 或者mcc filename.c 可以将cpp或者c源程序文件,编译为filename.mexw32(32位系统)或者filename.mexw64(64位系统 ...

  6. [MFC美化] Skin++使用详解-使用方法及注意事项

    主要分为以下几个方面: 1.Skin++使用方法 2.使用中注意事项 一. Skin++使用方法 SkinPPWTL.dll.SkinPPWTL.lib.SkinPPWTL.h ,将三个文件及相应皮肤 ...

  7. Objective-C Runtime 运行时之六:拾遗(转载)

    前面几篇基本介绍了runtime中的大部分功能,包括对类与对象.成员变量与属性.方法与消息.分类与协议的处理.runtime大部分的功能都是围绕这几点来实现的. 本章的内容并不算重点,主要针对前文中对 ...

  8. 创建Properties文件

    /** * 生成Properties文件 * @param map Properties文件的内容,键值对 * @param path Properties文件生成后存放的路径 * @param pr ...

  9. 低功耗蓝牙BLE外围模式(peripheral)-使用BLE作为服务端

    低功耗蓝牙BLE外围模式(peripheral)-使用BLE作为服务端 Android对外模模式(peripheral)的支持 从Android5.0开始才支持 关键术语和概念 以下是关键BLE术语和 ...

  10. hdu1032

    #include <iostream> using namespace std; int main() { int a,b,t,i,max; while(cin >> a &g ...