UVA - 536 Tree Recovery (二叉树重建)
题意:已知先序中序,输出后序。
#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 (二叉树重建)的更多相关文章
- UVa 536 Tree Recovery(二叉树后序遍历)
Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...
- 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 ...
- UVa 536 Tree Recovery
题意:给出一颗二叉树的前序遍历和中序遍历,输出其后序遍历 用杭电1710的代码改一点,就可以了. #include<iostream> #include<cstdio> #in ...
- UVA 536 Tree Recovery 建树+不建树
题意: 给出先序和中序,求后序. 思路: ①建树然后递归输出. //建树的 #include<iostream> #include<cstdio> #include<qu ...
- 【UVA】536 Tree Recovery(树型结构基础)
题目 题目 分析 莫名A了 代码 #include <bits/stdc++.h> using namespace std; string s1,s2; void buil ...
- POJ 2255 Tree Recovery 二叉树的遍历
前序和中序输入二叉树,后序输出二叉树:核心思想只有一个,前序的每个根都把中序分成了两部分,例如 DBACEGF ABCDEFG D把中序遍历的结果分成了ABC和EFG两部分,实际上,这就是D这个根的左 ...
- UVa 548 Tree【二叉树的递归遍历】
题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...
- POJ 2255 Tree Recovery 二叉树恢复
一道和Leetcode的一道题目基本上一样的题目. 给出前序遍历和中序遍历序列,要求依据这些信息恢复一颗二叉树的原貌,然后按后序遍历序列输出. Leetcode上有给出后序和中序,恢复二叉树的. 只是 ...
- POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)
链接:poj.org/problem?id=2255 本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html 题意: 分别给你一个二叉树的前序遍历序列和中序 ...
随机推荐
- SystemVerilog for design 笔记(二)
转载请标明出处 1. System Verilog文本值和数据类型 1.1. 增强的文本值赋值 相对于verilog,SV在文本值赋值时可以1.无需指定进制 2.赋值可以是逻辑1 用法: reg ...
- springboot之快速创建项目
1.选择创建新项目: 2.选择spring initializr,然后next 3.填写项目元数据,然后next 4.选择项目依赖,然后next 5.点击finish,完成项目创建
- Eclipse中创建新的SpringBoot项目(打包并且部署到tomcat)
Spring-boot因为其对jar包的高度集成以及简化服务配置,快速部署等的优点,逐渐成为Java开发人员的热衷的框架.下面演示一下怎么在Eclipse中新建Spring-boot项目以及打包部署. ...
- [c#]如何访问 JArray 的元素
JArray 格式文件. public void TestJson() { var jsonString = @"{""trends"": [ { & ...
- Myeclipse 安装时候android adt, android sdk常见问题
离线版adt安装 可以随意百度adt下载 安装时候注意断网模式,否则会连接到服务器耗费很长时间:如果安装报错,可能是adt与Myeclipse版本不匹配,如我用的是Myeclipse8.6,安装AD ...
- 统计学习方法——KD树最近邻搜索
李航老师书上的的算法说明没怎么看懂,看了网上的博客,悟出一套循环(建立好KD树以后的最近邻搜索),我想应该是这样的(例子是李航<统计学习算法>第三章56页:例3.3): 步骤 结点查询标记 ...
- vue中使用矢量图
1.打开矢量图库,将需要的图表添加至购物车 2.将购物车的图标添加到一个项目中(便于后期增加更新)并下载至本地 3.将这四个文件及iconfont.css添加至项目的assets中 4.打开iconf ...
- AC自动机 (模板)
AC自动机是用来干什么的: AC自动机是用来解决多模匹配问题,例如有单词s1,s2,s3,s4,s5,s6,问:在文本串ss中有几个单词出现过,类似. AC自动机实现这个功能需要三个部分: 1.将所有 ...
- Problem A: Assembly Required K路归并
Problem A: Assembly Required Princess Lucy broke her old reading lamp, and needs a new one. The cast ...
- Ternsorflow 学习:003-MNIST入门有关概念
前言 当我们开始学习编程的时候,第一件事往往是学习打印"HelloWorld".就好比编 程入门有 HelloWorld,机器学习入门有 MNIST. MNIST 是一个入门级的计 ...