这是一个很经典的建树,然而当时不会!!!!

给你一个中序和后序 先建一个二叉树,然后找最优解(最优解就是一个叶子节点到根节点权值最小, 同时本身权值最小)

//生成一棵树

int build(int L1, int R1, int L2, int R2) {	//表示1为中序,2为后序 生成这个二叉树  其中R2为这棵树的根
if(L1 > R1) return 0;
int root = post_order[R2];
int p = L1;
while(in_order[p] != root) ++p; //在中序中找到根的位置
int cnt = p - L1; //cnt表示当前这棵树左子树的大小
lch[root] = build(L1, p-1, L2, L2 + cnt -1);//这里要知道在后序中,左子树(cnt),右子树,根
rch[root] = build(p+1, R1, L2+cnt, R2-1); //那么我们就能知道他们的划分位置,继续递归生成树
return root;
}

//遍历这棵树

int best,best_sum;

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);
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
using namespace std;
const int maxn = 1e4+100;
int in_order[maxn], post_order[maxn], lch[maxn], rch[maxn];
int n;
bool read_list(int* a) {
string line;
if(!getline(cin, line)) return false;
stringstream ss(line);
n = 0;
int x;
while(ss>>x) a[n++] = x;
return n > 0;
} int build(int L1, int R1, int L2, int R2) {
if(L1 > R1) return 0;
int root = post_order[R2];
int p = L1;
while(in_order[p] != root) ++p;
int cnt = p - L1;
lch[root] = build(L1, p-1, L2, L2 + cnt -1);
rch[root] = build(p+1, R1, L2+cnt, R2-1);
return root;
} int best,best_sum; 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(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
while(read_list(in_order)) {
read_list(post_order);
build(0, n-1, 0, n-1);
best_sum = 1000000000;
dfs(post_order[n-1],0);
cout << best << "\n";
}
return 0;
}

UVA_548Tree的更多相关文章

随机推荐

  1. SSDT and Shadow SSDT table

    参考:http://x86.renejeschke.de/html/file_module_x86_id_313.html http://msdn.microsoft.com/en-us/librar ...

  2. __attribute__ ((packed))字节对齐

    1. __attribute__ ((packed)) 的作用就是告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐,是GCC特有的语法.这个功能是跟操作系统没关系,跟编译器有关,g ...

  3. Python3调用hessian

    领导派了个任务,实现服务器日志文件调用hessian接口保存到数据库 研究了半天python调用hessian的办法 首先使用hessian for python的链接: http://hessian ...

  4. python面试题之请谈谈.pyc文件和.py文件的不同之处

    虽然这两种文件均保存字节代码,但.pyc文件是Python文件的编译版本,它有平台无关的字节代码,因此我们可以在任何支持.pyc格式文件的平台上执行它.Python会自动生成它以优化性能(加载时间,而 ...

  5. POJ 2417 Discrete Logging ( Baby step giant step )

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3696   Accepted: 1727 ...

  6. qt 如何使用 lamda 表达式接收线程中发射的数据,并在里面更新 UI ?

    Qt 信号和槽连接方式 常量 描述 Qt::AutoConnection (默认)如果接收方位于发出信号的线程中,则使用Qt::DirectConnection.否则,使用Qt::QueuedConn ...

  7. python3.6:AttributeError: 'generator' object has no attribute 'next'

    环境:PyCharm+Anaconda python版本:3.6 协程测试: #!/usr/bin/env python # -*- coding:utf-8 -*- import time def ...

  8. data-*存数据,拿出ul li中的数据

    <ul class="questions"> <li> <div class="question">1.您的年龄是?< ...

  9. js 动态绑定解绑事件

    function addEvent(obj, type, handle) { if (obj.addEventListener) { obj.addEventListener(type, handle ...

  10. Nginx之Keepalived

    目录 Nginx之Keepalived 1. Keepalived 高可用基本概述 1.1 什么是高可用 1.2 高可用通常使用什么软件? 1.3 keepalived是如何实现高可用的? 1.4 那 ...