传送门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. sed常见用法,删除匹配行的上2行,下3行

    删除匹配的下一行到最后一行 [root@test200 ~]# cat test a b c d e f [root@test200 ~]# sed '/c/{p;:a;N;$!ba;d}' test ...

  2. ASP.NET CORE 1.0 初次接触

    vs2015 update3 升级后,可以创建asp.net core 1.0 的web应用了, 默认模版,发布到指定文件夹 服务器上需要安装 DotNetCore.1.0.0-WindowsHost ...

  3. AngularJS2之本地环境搭建

    前言:本来准备初探AngularJS2,结果成了复习git和再探node git的两个常见问题:一.github上传时出现error: src refspec master does not matc ...

  4. 个性化推荐系统中的BadCase分析

    针对内测用户反馈,由于前一天点击了几个动画,导致第二天推荐的动画屏占比较高,于是开始对此badcase进行分析. 首先分析了该用户的历史观看纪录,由于系统升级,日志缺陷问题,导致该用户10.15-11 ...

  5. mysql 时间

    显示当前时间: mysql> select now(); +---------------------+ | now() | +---------------------+ | -- :: | ...

  6. [NYLG-OJ] 77 开灯问题(白书例题)

    #include<stdio.h> int main() { int a[1010]={0}; //储存灯的开闭情况 int n,k,i,j; scanf("%d%d" ...

  7. JavaScript-Curry

    Currying allows you to easily create custom functions by partially invoking an existing function. He ...

  8. Linux 文件服务---------- nfs Server

    Linux 文件服务nfs (Network file system)#网络文件系统 ---> 远程文件调用samba #文件共享(unix /linux /windows ) ,只能适用于局域 ...

  9. iOS开发之视差滚动视图

    首先声明一点,由于自己iOS开发经验有限,这里给下面将要实现的效果起名叫视差滚动视图,自己也不知道是否严谨,等以后有经验了,再来更新吧. 一.需求 有的时候我们可能会有这样一种需求,在一个UITabl ...

  10. swift UILabel加载html源码

    @IBOutlet weak var content: UILabel! func setup(content:String){ self.content.preferredMaxLayoutWidt ...