UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS)
题意分析
给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小。若有多个,输出叶子节点本身权值小的那个节点。
先递归建树,然后DFS求解。
代码总览
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <vector>
#define nmax 100000
#define INF 0x3f3f3f3f
using namespace std;
int inorder[nmax],postorder[nmax];
int lnode[nmax],rnode[nmax];
int i;
bool read_input()
{
memset(inorder,0,sizeof(inorder));
memset(postorder,0,sizeof(postorder));
string s;
if(!getline(cin,s)) return false;
else{
stringstream ss(s);
i = 0;
int x;
while(ss >> x) inorder[i++] = x;
getline(cin,s);
stringstream sss(s);
i = 0;
while(sss>>x) postorder[i++] = x;
}
return true;
}
// 递归建树
int buildtree(int l1,int r1, int l2, int r2)
{
if(l1>r1) return 0;
int root = postorder[r2];
int p = l1;
while(inorder[p] != root) p++;
int cnt = p-l1;
lnode[root] = buildtree(l1,p-1,l2,l2+cnt-1);
rnode[root] = buildtree(p+1,r1,l2+cnt,r2-1);//
return root;
}
int bestsum,best;
void dfs(int u, int sum)
{
sum+=u;
if(!lnode[u] && !rnode[u])
if(sum<bestsum || (sum == bestsum && u<best)){
best = u;
bestsum = sum;
}
if(lnode[u]) dfs(lnode[u],sum);
if(rnode[u]) dfs(rnode[u],sum);
}
int main()
{
//freopen("in.txt","r",stdin);
while(read_input()){
bestsum = INF;best = INF;
buildtree(0,i-1,0,i-1);
dfs(postorder[i-1],0);
cout<<best<<"\n";
}
return 0;
}
UVA.548 Tree(二叉树 DFS)的更多相关文章
- UVa 548 Tree(二叉树最短路径)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- UVa 548 Tree【二叉树的递归遍历】
题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...
- uva 548 Tree(通过后序,先序重建树+dfs)
难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...
- Tree UVA - 548(二叉树递归遍历)
题目链接:https://vjudge.net/problem/UVA-548 题目大意:给一颗点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序遍历和后序遍历,找一个叶子结点使得它到根 ...
- 【紫书】Tree UVA - 548 静态建树dfs
题意:给你中序后序 求某叶子节点使得从根到该节点权值和最小.若存在多个,输出其权值最小的那个. 题解:先建树,然后暴力dfs/bfs所有路径,取min 技巧:递归传参数,l1,r1,l2,r2, su ...
- UVA - 548 Tree(二叉树的递归遍历)
题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #inc ...
- UVA 548(二叉树重建与遍历)
J - Tree Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Ap ...
- Uva 548 Tree
0.这是一道利用中序遍历和后序遍历确定二叉树的题目,学会建树 关键点理解这段代码 int build(int L1,int R1,int L2,int R2) { //printf("bui ...
- UVa 548 Tree (建树+前序后序)
Description You are to determine the value of the leaf node in a given binary tree that is the termi ...
随机推荐
- unity3d 计时功能舒爽解决方案
上次也写了一篇计时功能的博客 今天这篇文章和上次的文章实现思路不一样,结果一样 上篇文章地址:http://www.cnblogs.com/shenggege/p/4251123.html 思路决定一 ...
- 「日常训练」All Friends(POJ-2989)
题意 分析 代码 #include <iostream> #include <cstring> #include <algorithm> #define MP ma ...
- JVM监控远程服务器
1. 首先配置服务器端,进入服务器tomcat的bin目录下,打开catalina.sh配置文件,xxx为服务器配置路径. # cd /xxx/apache-tomcat-/bin # vim cat ...
- CentOS 7.2使用tomcat部署jenkins2.130
一.jenkins介绍 Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台.这是一个免费的源代码,可以处理任何类型的构建或持续集成.集成Jenkins可以用于一些测 ...
- 关于maven项目中修改的JS不生效的解决方案
1. 问题描述 昨天下午博主在开发学习的过程中,碰到一个修改了JS无法生效的问题,折腾我不少的时间,现将百度到的解决方案总结一下,以便下次碰到类似问题能够最快的找到解决方案 2 解决方案 2.1 方案 ...
- python3 SQLAlchemy模块使用
更详细的操作介绍:https://www.imooc.com/article/22343 定义: SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对 ...
- 技本功丨知否知否,Redux源码竟如此意味深长(上集)
夫 子 说 元月二号欠下袋鼠云技术公号一篇关于Redux源码解读的文章,转眼月底,期间常被“债主”上门催债.由于年底项目工期比较紧,于是债务就这样被利滚利.但是好在这段时间有点闲暇,于是赶紧把这篇文章 ...
- Python3 集合
1.集合的表示 集合是一个无序不重复的元素序列 创建空集合 set() 2.集合的运算 a={1,2,3} b={2,3,4} print(a-b) #a中包含b中不包含 print(a|b) #a中 ...
- VUE中组件的使用
关于vue组件引用 使用Nodejs的方法 被引用的组件要暴露 module.exports={}; 引用时 用 var abc= require("组件的路径") 然后 就可以用 ...
- 调试Python的方式
调试Python有如下几种方式: 1 使用print语句 2 使用IDE的debuggers 3 使用命令行调试器pdb,这是Python的一个标准库,类似gdb 4 使用-i命令行选项.在使用命令行 ...