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 题意: 分别给你一个二叉树的前序遍历序列和中序 ...
随机推荐
- Linux命令:sed命令
sed是一种流编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏 ...
- 使用jquery select2实现下拉框搜索功能
由于公司后台系统下拉框数据量太多了,用户操作起来要不方便所以增加了下拉框里面一个搜索功能 1从官网下载jquery select2 下来 地址https://select2.github.io/ 2: ...
- MYSQL 查询指定日期后的15天
SELECT s.id, s.exhi_id, s.commission, s.order_main_id, s.create_date, s.fen_xiao_id, s.state, s.upda ...
- Linux下杀掉所有得java进程
--转自https://blog.csdn.net/oppo62258801/article/details/81434038 1.Linux查看所有Java进程 ps -ef | grep java ...
- SpringBoot之日志记录-专题四
SpringBoot之日志记录-专题四 六.日志管理 6.1使用log4j记录日志 6.1.2新建log4j配置文件 文件名称log4j.properties #log4j.rootLogger=CO ...
- RIFF
RIFF全称为资源互换文件格式(Resources Interchange File Format),是Windows下大部分多媒体文件遵循的一种文件结构. RIFF文件所包含的数据类型由该文件的扩展 ...
- How to add VTL tapes on DD6300 & label them into "Oracle_VTL" pool
Dear all , This is liulei , I was back so , How to add VTL tapes on DD6300 & label them into ...
- 【动画演示】:JS 作用域链不在话下
作者:Lydia Hallie译者:前端小智来源:dev 点赞再看,养成习惯 本文 GitHub https://github.com/qq44924588... 上已经收录,更多往期高赞文章的分类, ...
- HTTP报文结构及Cookie、session区别
目录 万维网 HTTP超文本传输协议 特点 HTTP的报文结构. 下面介绍http请求报文最主要的一些特点 在服务器上存放用户的信息(Cookie) 实例: 工作原理: cookie中的主要内容: C ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:表示一个危险动作的按钮操作
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...