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(二叉树最短路径)的更多相关文章

  1. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  2. UVa 548 Tree【二叉树的递归遍历】

    题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...

  3. Tree UVA - 548(二叉树递归遍历)

    题目链接:https://vjudge.net/problem/UVA-548 题目大意:给一颗点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序遍历和后序遍历,找一个叶子结点使得它到根 ...

  4. UVA - 548 Tree(二叉树的递归遍历)

    题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #inc ...

  5. UVA 548(二叉树重建与遍历)

    J - Tree Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Ap ...

  6. Uva 548 Tree

    0.这是一道利用中序遍历和后序遍历确定二叉树的题目,学会建树 关键点理解这段代码 int build(int L1,int R1,int L2,int R2) { //printf("bui ...

  7. uva 548 Tree(通过后序,先序重建树+dfs)

    难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...

  8. UVa 548 Tree (建树+前序后序)

    Description You are to determine the value of the leaf node in a given binary tree that is the termi ...

  9. UVa 548 Tree(中序遍历+后序遍历)

    给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...

随机推荐

  1. html to docx

    public static void main(String[] args) throws Exception{ //创建 POIFSFileSystem 对象 POIFSFileSystem poi ...

  2. Sql Server中日期时间格式化为字符串输出

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  3. B树、B-树、B+树、B*树的定义和区分

    MySQL是基于B+树聚集索引组织表 B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右 ...

  4. DOTWeen 使用

    using UnityEngine; using System.Collections; using DG.Tweening; using UnityEngine.UI; public class T ...

  5. flume 详细介绍

    http://blog.csdn.net/a2011480169/article/details/51544664 配有详细的例子. http://www.cnblogs.com/gongxijun/ ...

  6. 基于Java SE集合的图书管理系统

    图书管理系统一.需求说明1.功能:登录,注册,忘记密码,管理员管理,图书管理.2.管理员管理:管理员的增删改查.3.图书管理:图书的增删改查.4.管理员属性包括:id,姓名,性别,年龄,家庭住址,手机 ...

  7. 解决:python 连接Oracle 11g 报错:ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务

    其次,将查询到的service_name替换sid即可:conn=cx_Oracle.connect('hr/admin@localhost:1521/EE.oracle.docker')

  8. 学JS的心路历程-Promise(二)

    昨天有说到Promise的创建以及then的用法,今天我们来看错误处理. then onRejected 我们昨天有提到说,then两个函式参数,onFulfilled和onRejected,而onR ...

  9. 税控服务器 TC5002UpdatePackage 安装更新

    Linux版税控服务器单税号版本税控应用:   TC5002UpdatePackage2008160711.zip         单税号服务器(型号:TCG-01S1) Linux版税控服务器20个 ...

  10. js异步加载的5种方式

    方案1:$(document).ready 点评: 1.需要引用jquery 2.兼容所有浏览器. 方案2:<script>标签的async="async"属性 asy ...