【紫书】Tree UVA - 548 静态建树dfs
题意:给你中序后序 求某叶子节点使得从根到该节点权值和最小。若存在多个,输出其权值最小的那个。
题解:先建树,然后暴力dfs/bfs所有路径,取min
技巧:递归传参数,l1,r1,l2,r2, sum,root,
代码:
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include<stdio.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<iostream>
#include<string.h>
#include<queue>
#include<string>
#include<sstream>
using namespace std;
const int maxn = 1e4+;
string line; int n;
int a[maxn],in_order[maxn],post_order[maxn];
int lch[maxn], rch[maxn];
int mn,best;
bool read_list(int *a) {
if (!getline(cin, line)) return false;
stringstream ss(line);
n = ;
int x;
while (ss >> x)a[n++] = x;
return n > ;
}
int build(int l1, int r1, int l2, int r2) {//将中序l1,r1 将后序l2,r2 建成一棵树,返回树根
if (l1 > r1)return ;
int root = post_order[r2];
int p = l1;
while (in_order[p] != root)p++;
int cnt = p - l1;
lch[root] = build(l1, p - , l2, l2 + cnt - );
rch[root] = build(p + , r1, l2 + cnt, r2 - );
return root;
}
void dfs(int u, int sum) {
sum += u;
if (!lch[u] && !rch[u]) if (sum < mn || (sum == mn&&u < best)) {best = u; mn = sum;}
if (lch[u])dfs(lch[u], sum);
if (rch[u])dfs(rch[u], sum);
}
int main() {
while (read_list(in_order)) {
read_list(post_order);
build(, n - , , n - );
mn = 1e9;
dfs(post_order[n - ], );
cout << best << "\n";
} //system("pause");
}
【紫书】Tree UVA - 548 静态建树dfs的更多相关文章
- Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- UVA548——Tree(中后序建树+DFS)
Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...
- Tree UVA - 548
You are to determine the value of the leaf node in a given binary tree that is the terminal node o ...
- 树(Tree,UVA 548)
题目描述: 题目思路: 1.使用数组建树 //递归 2.理解后序遍历和中序遍历,建立左右子树 3.dfs深度搜索找出权重最小的路径 #include <iostream> #include ...
- Tree UVA - 548(二叉树递归遍历)
题目链接:https://vjudge.net/problem/UVA-548 题目大意:给一颗点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序遍历和后序遍历,找一个叶子结点使得它到根 ...
- UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)
Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- UVa 548 Tree【二叉树的递归遍历】
题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...
- 紫书 例题 11-13 UVa 10735(混合图的欧拉回路)(最大流)
这道题写了两个多小时-- 首先讲一下怎么建模 我们的目的是让所有点的出度等于入度 那么我们可以把点分为两部分, 一部分出度大于入度, 一部分入度大于出度 那么显然, 按照书里的思路,将边方向后,就相当 ...
随机推荐
- 8 -- 深入使用Spring -- 4...4 Spring 的 AOP 支持
8.4.4 Spring 的AOP 支持 Spring中的AOP代理由Spring的IoC容器负责生成.管理,器依赖关系也由IoC容器负责管理.因此,AOP代理可以直接使用容器中的其他Bean实例作为 ...
- 给button添加边框和圆角
button是我们经常用到的控件,我把它的属性罗列一下: UIButton *Button = [[UIButton alloc] initWithFrame:CGRectMake(, , , )]; ...
- Fiddler 抓取 Genymotion 数据包
对genymotion进行如下设置
- Netty权威指南之NIO通信模型
NIO简介:与Socket和ServerSocket类相对应,NIO提供了SocketChannel和ServerSocketChannel两种不同的套接字通道实现,这两种新通道都支持阻塞和非阻塞两种 ...
- 【PHP】phpstudy vhosts.conf 配置
#Listen 876 <VirtualHost *:876> ServerName localhost DocumentRoot "D:\phpStudy\PHPTutoria ...
- Windows 2012 R2 安装Nessus
1.nessus官网注册 注册地址:https://www.tenable.com/products/nessus-home Name字段随意,邮箱需要填写自己的,方便接受注册码 2.注册后,登录邮箱 ...
- samba 服务器
1.apt-get install smaba 2.安装完成后apt-get install smbclient 然后就是配置那个.conf文件,这个到网上搜下ubuntu下配置smaba服务器就可以 ...
- 顶点纹理shader
Shader "Custom/VertDisplace" { Properties { _MainTex ("Base (RGB)", 2D) = " ...
- gearman 简介
附件 Gearman.doc 1:介绍gearman 1.1 简介 Gearman是一个用来把工作委派给其他机器.分布式的调用更适合做某项工作的机器.并发的做某项工作在多个调用间做负载均衡.或用来在 ...
- .net页面生命周期【转】
.Net 页面生命周期IIS 所收到的对某 Microsoft ASP.NET 页面的每个请求都被移交给 ASP.NET HTTP 管线.HTTP 管线由一系列托管对象组成,这些对象按顺序处理该请求, ...