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. confusing c++ 重写 与 重定义 记录1

    class parent { public: void f() { cout << "parent f()" << endl; } void f(int i ...

  2. 如何让cxgrid自动调整列宽

    1.选中cxgridview,在属性中找OptionsView--->ColumAutoWidth,把这个属性设为True; 2.在FDMemtable的open之后加上如下代码即可 [delp ...

  3. java学习大方向

    总结Java程序员成长之路   转载  http://bbs.javazhijia.com/topic/1bb0733f80d94aedb50cc3b66d9792b6.html 我也搞了几年JAVA ...

  4. linux 再多的running也挡不住锁

    再续<linux 3.10 一次softlock排查>,看运行态进程数量之多: crash> mach MACHINE TYPE: x86_64 MEMORY SIZE: GB CP ...

  5. Java swing 项目写成bat文件

    java  -Dfile.encoding=GBK -Xms512m -Xmx512m -cp .;.\lib\*  com.bozhirui.show.TableIn 以上为bat 文件的所有内容 ...

  6. 23.网络.md

    目录 1.基本概念 2.常用函数 3.端口 4.协议 4.1.2代码步骤 4.1.3UDPDemo 4.1.4通信格式 4.1.5 群发Demo: 4.1.6丢失数据的情况 4.2TCP 4.2.1三 ...

  7. 前端Web安全介绍及规避。。。

    本文转载自:https://jelon.top/posts/web-security/ 如果侵权,请及时告知. 一.跨站脚本攻击 (xss) 反射型跨站脚本攻击 攻击者会通过社会工程学手段,发送一个 ...

  8. linux基本命令练习

    1. 熟悉linux命令并且练习用法以及应用场景. 初学者完成Linux系统分区及安装之后,需熟练掌握Linux系统管理必备命令,命令包括:cd.ls.pwd.clear. chmod.chown.c ...

  9. 学JS的心路历程 - PixiJS -基础(三)

    今天我们来试着移动图片吧! 首先,一样先把图片放到PIXI的stage中: let app = new PIXI.Application({ width: 800, height: 600, back ...

  10. TensorFlow 辨异 —— tf.add(a, b) 与 a+b(tf.assign 与 =)、tf.nn.bias_add 与 tf.add(转)

    1. tf.add(a, b) 与 a+b 在神经网络前向传播的过程中,经常可见如下两种形式的代码: tf.add(tf.matmul(x, w), b) tf.matmul(x, w) + b 简而 ...