UVa 548 树(已知其中两种遍历, 还原树)
题意:
给出后序遍历和先序遍历, 还原一棵树, 然后求出从根节点到叶子的最小路劲和。
分析:

已知后序遍历, 那么后序的最后一个节点就是根节点, 然后在中序中找到这个节点, 它的左边就是左子树, 它的右边就是右子树, 然后递归下去。
技巧是不断的变动[r1,l1] [r2,l2]
r1 l1是中序的区间 r2 l2是后序的区间
#include <bits/stdc++.h>
using namespace std;
const int maxn = + ;
int In_order[maxn], Post_order[maxn], lch[maxn], rch[maxn];
int tot, cnt;
//[L1,R1] , [L2,R2];
bool read(int* a){
string t;
if(!getline(cin, t))
return false;
int n = , x;
stringstream ss(t);
while(ss >> x) a[n++] = x; tot = n;
if(n == ) return false;
else return true;
}
int bulid(int L1, int R1, int L2, int R2){
if(R1 < L1) { return ;}
int root = Post_order[R2];
int p = L1;
while(In_order[p] != root) p++;
int pcnt = p - L1;
lch[root] = bulid(L1, p-,L2, L2 + pcnt -);
rch[root] = bulid(p+, R1,L2 + pcnt, R2 - );
return root;
}
int best_sum = 1e9, best;
void dfs(int u, int sum)
{
sum += u;
if(!lch[u] && !rch[u]){
if(sum < best_sum || (sum == best_sum && u < best)){
best = u; best_sum = sum;
}
}
if(lch[u]){
dfs(lch[u],sum);
}
if(rch[u]){
dfs(rch[u],sum);
}
}
int main(){
while(read(In_order)){
memset(lch,,sizeof(lch));
memset(rch,,sizeof(rch));
best_sum = 1e9, best = ;
read(Post_order);
cnt = ;
bulid(,tot-,,tot-);
dfs(Post_order[tot-],);
printf("%d\n", best);
}
return ;
}
UVa 548 树(已知其中两种遍历, 还原树)的更多相关文章
- POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...
- php 两种获取分类树的方法
php 两种获取分类树的方法 1. /** * 获取分类树 * @param array $array 数据源 * @param int $pid 父级ID * @param int $level 分 ...
- HashMap的两种遍历方式
HashMap的两种遍历方式 HashMap存储的是键值对:key-value . java将HashMap的键值对作为一个整体对象(java.util.Map.Entry)进行处理,这优化了Hash ...
- Map的两种遍历方式
********************************************************************************* ****************** ...
- POJ 3225 线段树区间更新(两种更新方式)
http://blog.csdn.net/niuox/article/details/9664487 这道题明显是线段树,根据题意可以知道: (用0和1表示是否包含区间,-1表示该区间内既有包含又有不 ...
- 已知有两个水杯,一个11L一个7L,水可以任意使用,求怎么得到2L 的详细解法
问题:有两个水杯,一个是11L一个是7L,水可以随便用,怎么得到2L 1.了解问题的本质 问题中给出了两个杯子,只有这两个杯子有量度,所以只能让杯中的水满进满出才能确定杯子中最后有多少水. 现在问题要 ...
- Delphi 查找标题已知的窗口句柄,遍历窗口控件句柄(转)
用我的方法来控制其他程序窗体上的窗口控件,必须先了解什么是 回调函数.我的理解是这样的: 回 调函数写出来不是自己的程序去调用的,反而是让其他的东西去调用,比如windows操作系统,比如其他的程序等 ...
- HashMap两种遍历方式的深入研究
转自:http://swiftlet.net/archives/1259 HashMap的遍历有两种方式,如下所示:第一种利用entrySet的方式: 1 2 3 4 5 6 7 Map map ...
- HashMap两种遍历数据的方式
HashMap的遍历有两种方式,一种是entrySet的方式,另外一种是keySet的方式. 第一种利用entrySet的方式: Map map = new HashMap(); Iterator i ...
随机推荐
- 记一次线上环境的内存溢出(java.lang.OutOfMemoryError)
事故背景 今天客户说风控项目有个别用户查询不到数据不是报错就是一直卡在那里,我就去那个接口看了下. 一看项目日志今天的都几个g了,平常也就几百兆吧,很明显出了问题. 请求接口后使用命令tail -f ...
- java-异常简介
1.简介 ############################################################### ############################### ...
- 题解报告:hdu 1060 Leftmost Digit
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1060 问题描述 给定一个正整数N,你应该输出N ^ N的最左边的数字. 输入 输入包含多个测试用例. ...
- Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/NamespaceContext
使用dom4j的xpath查询节点,报如下错误: Exception in thread "main" java.lang.NoClassDefFoundError: org/ja ...
- Android 线程池系列教程(5)与UI线程通信要用Handler
Communicating with the UI Thread 上一课 下一课 1.This lesson teaches you to Define a Handler on the UI Thr ...
- 220 Contains Duplicate III 存在重复 III
给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使 nums [i] 和 nums [j] 的绝对差值最大为 t,并且 i 和 j 之间的绝对差值最大为 k. 详见:https://le ...
- E. Comments dfs模拟
http://codeforces.com/contest/747/problem/E 首先,把字符串变成这个样子. hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0 ...
- shell脚本中定义路径变量出现的BUG
=========================================================================== if 语句中的定义路径变量 引发命令的PATH路 ...
- (四)SpringIoc之Bean装配
在pom.xml的依赖 <dependencies> <!--测试包--> <dependency> <groupId>junit</groupI ...
- hihocoder offer收割编程练习赛11 A hiho字符串
思路: 我用的尺取. 注意题目描述为恰好2个'h',1个'i',1个'o'. 实现: #include <iostream> #include <cstdio> #includ ...