题意:已知先序中序,输出后序。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
char pre_order[MAXN];
char in_order[MAXN];
int leftchild[MAXN];
int rightchild[MAXN];
int build(int L1, int R1, int L2, int R2){
if(L1 > R1) return ;
int root = pre_order[L1] - 'A' + ;
int st = L2;
while((in_order[st] - 'A' + ) != root) ++st;
int cnt = st - L2;
leftchild[root] = build(L1 + , L1 + cnt, L2, L2 + cnt - );
rightchild[root] = build(L1 + + cnt, R1, st + , R2);
return root;
}
void dfs(int root){
if(leftchild[root]) dfs(leftchild[root]);
if(rightchild[root]) dfs(rightchild[root]);
printf("%c", root + 'A' - );
}
int main(){
while(scanf("%s", pre_order) != EOF){
scanf("%s", in_order);
int len = strlen(pre_order);
build(, len - , , len - );
int root = pre_order[] - 'A' + ;
dfs(root);
printf("\n");
}
return ;
}

UVA - 536 Tree Recovery (二叉树重建)的更多相关文章

  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 | GOJ 1077 Post-order (习题 6-3)

    传送门1: https://uva.onlinejudge.org/external/5/536.pdf 传送门2: http://acm.gdufe.edu.cn/Problem/read/id/1 ...

  3. UVa 536 Tree Recovery

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

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

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

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

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

  6. POJ 2255 Tree Recovery 二叉树的遍历

    前序和中序输入二叉树,后序输出二叉树:核心思想只有一个,前序的每个根都把中序分成了两部分,例如 DBACEGF ABCDEFG D把中序遍历的结果分成了ABC和EFG两部分,实际上,这就是D这个根的左 ...

  7. UVa 548 Tree【二叉树的递归遍历】

    题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...

  8. POJ 2255 Tree Recovery 二叉树恢复

    一道和Leetcode的一道题目基本上一样的题目. 给出前序遍历和中序遍历序列,要求依据这些信息恢复一颗二叉树的原貌,然后按后序遍历序列输出. Leetcode上有给出后序和中序,恢复二叉树的. 只是 ...

  9. POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)

    链接:poj.org/problem?id=2255 本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html 题意: 分别给你一个二叉树的前序遍历序列和中序 ...

随机推荐

  1. CentOS 7 启动流程

    1.UEFI或BIOS初始化,运行POST开机自检 2.选择启动设备,Boot Sequence 系统启动次序一般含有:光驱.U盘.硬盘A.硬盘B.按次序查找各引导设备,第一个有引导程序的设备即为本次 ...

  2. springmvc 请求出现400错误(当传入的参数类型是Date时加上下面代码试试)

    @InitBinder protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) t ...

  3. JSTL fn:replace()函数替换 换行符

    转自:http://blog.163.com/chenjie_8392/blog/static/439339842010513128139/ 近日在使用textarea时,输入了回车,为了将texta ...

  4. js获取video的播放时长

      setTimeout(function () { var vid = document.getElementById("videoIntro"); var currentTim ...

  5. rundll32.exe文件详解

    平时很常听到有些朋友说:呀,我系统的注册表启动项目有rundll32.exe,系统进程也有rundll32.exe,是不是病毒呀?这是对rundll32.exe接口不了解,其实其原理非常简单,了解并掌 ...

  6. eclipse 编辑窗口不见了(打开左边的java、xml文件,中间不会显示代码)

    参考:https://blog.csdn.net/u012062810/article/details/46729779

  7. save the transient instance before flushing错误解决办法

    错误原因: new了一个新对象,在未保存之前将它保存进了一个新new的对象(也即不是持久态). 解决办法: 在保存或更新之前把这个对象查出来(这样就是一个持久态) <set name=" ...

  8. 如果不想在django 的settings中保存mysql数据库的密码

    如题,你可以编写一个配置文件,用'OPTIONS' 将该配置文件导入进来,这样你发布到git上的源码上就没有你的数据库密码了. 这是django推荐的方法. # settings.py DATABAS ...

  9. Day3-M-Cable master POJ1064

    Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Commi ...

  10. Java服务端对Cookie的简单操作

    Java服务端对Cookie的简单操作 时间 2016-04-07 10:39:44 极客头条 原文  http://www.cuiyongzhi.com/index.php/post/15.html ...