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的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...
随机推荐
- 机器学习进阶-图像形态学操作-膨胀操作 1.cv2.dilate(进行膨胀操作)
1.cv2.dilate(src, kernel, iteration) 参数说明: src表示输入的图片, kernel表示方框的大小, iteration表示迭代的次数 膨胀操作原理:存在一个ke ...
- day02-格式化输出
python格式化字符串有%和{}两种 字符串格式控制符. 字符串输入数据格式类型(%格式操作符号) %%百分号标记%c字符及其ASCII码%s字符串%d有符号整数(十进制)%u无符号整数(十进制)% ...
- air 调用jsfl 执行对应函数,并传参
jsflPath = "WindowSWF/dt_tool_jsfl/" + event.item.fileName+".jsfl"; var element_ ...
- ubuntu 安装oracle客户端
from: http://webikon.com/cases/installing-oracle-sql-plus-client-on-ubuntu Installing Oracle SQL*Plu ...
- 一次UNITY闪退问题的定位心得
最近项目测试发现,运行unity后不退出运行模式,玩了一局后点击 “再来一局”,反复十几局后unity崩掉. 经观察,发现在这十几局的过程中,unity占用内存不断上升,由3.2G左右上升到3.6G左 ...
- 关于STRUCT优化的一个点
在西山居的这篇U3D cheatsheet中,提到: c12. 确保 struct 实现了 Equals() 和 GetHashCode() 这怎么理解? 首先,看下system.object.equ ...
- [PHP]对Json字符串解码返回NULL的一般解决方案
---------------------------------------------------------------------------------------------------- ...
- Windows下查看端口常用命令以及关闭端口的方法
1.C:\Users\JavaKam> netstat -ano|findstr 1099 TCP LISTENING TCP [::]: [::]: LISTENING 2.出现: C:\Us ...
- delphi 原创应用工具箱
用到的主要知识点: (1) listview背景透明 (2) 读取应用图标 (3)图标透明 (4)实时显示微软必应首页图,裁剪图片等
- LINUX系统一一CentOS6.5之安装JDK
准备工作 1.在/usr/目录下创建java目录 [root@localhost ~]# mkdir/usr/local/java/jdk[root@localhost ~]# cd /usr/loc ...