UVa 548 Tree(二叉树最短路径)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.
Input
The input file will contain a
description of the binary tree given as the inorder and postorder
traversal sequences of that tree. Your program will read two line (until
end of file) from the input file. The first line will contain the
sequence of values associated with an inorder traversal of the tree and
the second line will contain the sequence of values associated with a
postorder traversal of the tree. All values will be different, greater
than zero and less than 10000. You may assume that no binary tree will
have more than 10000 nodes or less than 1 node.
Output
For each
tree description you should output the value of the leaf node of a path
of least value. In the case of multiple paths of least value you should
pick the one with the least value on the terminal node.
Sample Input
3 2 1 4 5 7 6
3 1 2 5 6 7 4
7 8 11 3 5 16 12 18
8 3 11 7 16 18 12 5
255
255
Sample Output
1
3
255
题意
已知中序和后序,求叶节点到根节点的最短路径,若相同则叶节点小的优先,输出叶节点
题解
bfs找统计最优解就不说了,这是用递归做的
代码
#include<bits/stdc++.h>
using namespace std;
int Left[],Right[],In[],Post[];
int n,best,best_sum;
//把In[L1,R1],Post[L2,R2]建树
int build(int L1,int R1,int L2,int R2,int sum)
{
if(L1>R1)return ;//空树
int root=Post[R2];//后序最后一个节点为根
sum+=root;
int p=L1;//中序的头开始找根
while(In[p]!=root)p++;
int cnt=p-L1;//左子树个数
if(L1>p-&&p+>R1)//叶节点
{
if(sum<best_sum||(sum==best_sum&&root<best))
{
best=root;
best_sum=sum;
}
}
Left[root]=build(L1,p-,L2,L2+cnt-,sum);
Right[root]=build(p+,R1,L2+cnt,R2-,sum);
return root;//返回树根给左子树或右子树
}
int read(int *a)
{
string s;
if(!getline(cin,s))return ;
stringstream ss(s);
int x;
n=;
while(ss>>x)a[n++]=x;
return ;
}
int main()
{
while(read(In))
{
read(Post);
best_sum=1e9;
build(,n-,,n-,);
printf("%d\n",best);
}
return ;
}
UVa 548 Tree(二叉树最短路径)的更多相关文章
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- UVa 548 Tree【二叉树的递归遍历】
题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...
- Tree UVA - 548(二叉树递归遍历)
题目链接:https://vjudge.net/problem/UVA-548 题目大意:给一颗点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序遍历和后序遍历,找一个叶子结点使得它到根 ...
- 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(通过后序,先序重建树+dfs)
难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...
- UVa 548 Tree (建树+前序后序)
Description You are to determine the value of the leaf node in a given binary tree that is the termi ...
- UVa 548 Tree(中序遍历+后序遍历)
给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...
随机推荐
- HTML学习-2标记标签-2
三.表单元素 ①<form></form>表单标签,代表表单 主要属性:1.action提交到的页面. 2.method数据提交方式(get显示提交,有长度限制.post隐 ...
- ABAP-container拆分
1.界面 2.代码 *&---------------------------------------------------------------------* *& Report ...
- ejs 用到的语法
1.ejs 服务端渲染模板 2.语法: 01. <%= 变量名 %> -原样输出,不解析标签 02. <% js代码 %> 03. <%- 变量名%> -解析标签 ...
- vue:在router里面给页面加title
vue中给组件页面加页面标题:{ path: '/', name: 'index', component: disconnect, meta: { title: '首页' } }, { path: ' ...
- JSTL如何遍历Servlet传过来的list和map,用例子说明
后端 List<Article> list = dao.getPageList(nameid,Integer.parseInt(page)); request. ...
- Android Uri获取真实路径以及文件名的方法【转】
原文地址:https://blog.csdn.net/MikoGodZd/article/details/50979653 在Android 编程中经常会用到uri转化为文件路径 下面是4.4后通过U ...
- js(鼠标键盘拖动事件)
拖动事件是h5(HTML5的) 1:draggable(true) 2:拖动源 ondragstart ,ondragend 3:目的地 ondraglenter,ondragover,ondragl ...
- linux最大允许的文件描述符open files数nofile修改
open file resource limit 是linux中process可以打开的文件句柄数量.增加这个数值需要调整两个配置: 第一步, 修改系统最大允许的文件描述符 查看当前的设置: $ ca ...
- 编程四剑客awk
awk 'pattern +{action}' file (1)AWK基本语法参数详解 a:单引号 ''是为了和shell命令区分开: b:大括号{}表示一个命令分组: c:pattern 是一个过 ...
- 辅助测试工具xip.io
http://xip.io/ https://github.com/basecamp/xip-pdns