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. Spring boot 错误处理机制

    请求方式时,若不存在 浏览器出现White label Error Page 错误页面 其他客户端出现响应一个JSON格式文本包含错误码等信息 浏览器发送请求的请求头: 客户端请求头 这样就能区分来自 ...

  2. Win2008R2配置WebDeploy(转)

    一.配置服务器 1.安装管理服务 2.点击管理服务进行配置 3.安装WebDeploy 3.1通过离线安装包方式安装: https://www.iis.net/downloads/microsoft/ ...

  3. cxGrid 颜色设置

    一.cxGrid 根据列值变色(样式) 在使用cxGrid的过程中,某一个单元格经常需要根据其他单元格的值来做相应的变色,如: 在cxGridDBTableView中,选定要变样式(如背景色.字体属性 ...

  4. Java学习路线(转)

    原文:http://www.hollischuang.com/archives/489 一.基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收 http ...

  5. linux 2.6.32文件系统 fd与file*之间的关系

    给定如下一定情况: crash> files PID: TASK: ffff8817facd2100 CPU: COMMAND: "slabtop" ROOT: / CWD: ...

  6. 二、消息队列之如何在C#中使用RabbitMQ(转载)

    二.消息队列之如何在C#中使用RabbitMQ 1.什么是RabbitMQ.详见 http://www.rabbitmq.com/. 作用就是提高系统的并发性,将一些不需要及时响应客户端且占用较多资源 ...

  7. [ SHELL编程 ] 远程服务器传输文件

    在shell编程中经常需要获取远程服务器文件.手工操作中使用scp命令完成.为避免脚本执行scp输入密码进行交互,需先建立本机服务器当前用户和远程服务器指定用户的信任关系.具体代码见操作实例,重点关注 ...

  8. webpack打包avalon+oniui+jquery

    随着avalon的发展壮大,我根据CSDN的统计数字,中国前端大概有1%的人在使用avalon了. avalon的最大优势是能兼容IE6,并且其API是非常稳定,只是在1.3.7 对ms-duplex ...

  9. Node稳定性的研究心得

    目前大部分Web服务器,如Apache,都使用多线程的方式响应多用户请求,即一个线程服务一个用户请求.这种模式其中一个好处是,当某个请求的线程上抛出的异常没被捕获,只会影响当前这个线程,不会影响其他请 ...

  10. Glide4 高效加载图片的配置【转】

    原文: Glide4 高效加载图片的配置 https://www.jianshu.com/p/a079713f280f