题意:

给出后序遍历和先序遍历, 还原一棵树, 然后求出从根节点到叶子的最小路劲和。

分析:

已知后序遍历, 那么后序的最后一个节点就是根节点, 然后在中序中找到这个节点, 它的左边就是左子树, 它的右边就是右子树, 然后递归下去。

技巧是不断的变动[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 树(已知其中两种遍历, 还原树)的更多相关文章

  1. POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)

    题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...

  2. php 两种获取分类树的方法

    php 两种获取分类树的方法 1. /** * 获取分类树 * @param array $array 数据源 * @param int $pid 父级ID * @param int $level 分 ...

  3. HashMap的两种遍历方式

    HashMap的两种遍历方式 HashMap存储的是键值对:key-value . java将HashMap的键值对作为一个整体对象(java.util.Map.Entry)进行处理,这优化了Hash ...

  4. Map的两种遍历方式

    ********************************************************************************* ****************** ...

  5. POJ 3225 线段树区间更新(两种更新方式)

    http://blog.csdn.net/niuox/article/details/9664487 这道题明显是线段树,根据题意可以知道: (用0和1表示是否包含区间,-1表示该区间内既有包含又有不 ...

  6. 已知有两个水杯,一个11L一个7L,水可以任意使用,求怎么得到2L 的详细解法

    问题:有两个水杯,一个是11L一个是7L,水可以随便用,怎么得到2L 1.了解问题的本质 问题中给出了两个杯子,只有这两个杯子有量度,所以只能让杯中的水满进满出才能确定杯子中最后有多少水. 现在问题要 ...

  7. Delphi 查找标题已知的窗口句柄,遍历窗口控件句柄(转)

    用我的方法来控制其他程序窗体上的窗口控件,必须先了解什么是 回调函数.我的理解是这样的: 回 调函数写出来不是自己的程序去调用的,反而是让其他的东西去调用,比如windows操作系统,比如其他的程序等 ...

  8. HashMap两种遍历方式的深入研究

    转自:http://swiftlet.net/archives/1259 HashMap的遍历有两种方式,如下所示:第一种利用entrySet的方式:   1 2 3 4 5 6 7 Map map ...

  9. HashMap两种遍历数据的方式

    HashMap的遍历有两种方式,一种是entrySet的方式,另外一种是keySet的方式. 第一种利用entrySet的方式: Map map = new HashMap(); Iterator i ...

随机推荐

  1. 例题 3-5 生成元 digit generator

    #include<stdio.h> #include<string.h> #define maxn 100005 int ans[maxn]; //类似于 比较大的数组还是开导 ...

  2. 史上最详细最全的Linux上安装Oracle的教程-centos7

    一.安装Oracle前准备 1.创建运行oracle数据库的系统用户和用户组 [humf@localhost ~]$ su root #切换到root Password: [root@localhos ...

  3. Django中的cookie和session实现

    cookie from django.shortcuts import render, HttpResponse, redirect # 此装饰器的作用就是讲所有没有cookie验证的页面都需要验证后 ...

  4. openstack知识---hypervisor

    hypervisor Hypervisor是一种运行在物理服务器和操作系统之间的中间软件层,可允许多个操作系统和应用共享一套基础物理硬件,因此也可以看作是虚拟环境中的“元”操作系统,它可以协调访问服务 ...

  5. Service官方教程(7)Bound Service示例之1-同进程下直接继承Service

    Extending the Binder class If your service is used only by the local application and does not need t ...

  6. 转-解决Mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost'问题

    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)   Red Hat Enterpr ...

  7. mysql 中 时间函数 now() current_timestamp() 和 sysdate() 比较

    转载请注明出处 https://www.cnblogs.com/majianming/p/9647786.html 在mysql中有三个时间函数用来获取当前的时间,分别是now().current_t ...

  8. jdk线程池,使用手记

    Executors----------------------------------------------Executors------------------------------------ ...

  9. CSS定位内容

    div.h1 或 p 元素常常被称为块级元素.这意味着这些元素显示为    一块内容,即“块框”.与之相反,span 和 strong 等元素称为“行    内元素”,这是因为它们的内容显示在行中,即 ...

  10. Bmob使用心得

    1.在 Project 的 build.gradle 文件中添加 Bmob的maven仓库地址,示例如下:(注意文字说明部分): allprojects { repositories { jcente ...