uva 558 tree(不忍吐槽的题目名)——yhx
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.
#include<cstdio>
#include<cstring>
struct node
{
int lch,rch;
}a[];
int zx[],hx[],n,ans=0x7f7f7f7f,ans_leaf=0x7f7f7f7f;
bool rdzx()
{
int i,j,k,p,q,x,y,z;
char c;
if (scanf("%d",&zx[])==-) return ;
n=;
while (scanf("%c",&c)&&c==' ')
scanf("%d",&zx[++n]);
return ;
}
void rdhx()
{
int i;
for (i=;i<=n;i++)
scanf("%d",&hx[i]);
}
int crt(int lz,int rz,int lh,int rh)
{
if (lz>rz) return ; //注意终止条件
int i,j,k,p,q,x,y,z;
p=hx[rh];
i=lz;
while (zx[i]!=p) i++;
a[p].lch=crt(lz,i-,lh,i+lh-lz-); //关键在于这两个式子的推导,列方程求比较不费脑子
a[p].rch=crt(i+,rz,rh-rz+i,rh-);
return p;
}
void sc(int p,int x)
{
int i,j,k,l,m,y,z;
x+=p;
if (!a[p].lch&&!a[p].rch)
{
if (x<ans||(x==ans&&p<ans_leaf))
{
ans=x;
ans_leaf=p;
return;
}
}
if (a[p].lch) sc(a[p].lch,x);
if (a[p].rch) sc(a[p].rch,x);
}
int main()
{
int i,j,k,l,m,p,q,x,y,z;
while (rdzx())
{
rdhx();
memset(a,,sizeof(a));
crt(,n,,n);
ans=ans_leaf=0x7f7f7f7f;
sc(hx[n],);
printf("%d\n",ans_leaf);
}
}
递归求树,由于后序遍历的最后一个元素一定是根,所以可以在中序中找到根的位置,则其左边为左子树,右边为右子树,分治求解。
求值相对简单,dfs即可。
uva 558 tree(不忍吐槽的题目名)——yhx的更多相关文章
- UVA 558 判定负环,spfa模板题
1.UVA 558 Wormholes 2.总结:第一个spfa,好气的是用next[]数组判定Compilation error,改成nexte[]就过了..难道next还是特殊词吗 题意:科学家, ...
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- UVa 112 - Tree Summing(树的各路径求和,递归)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- uva 558 - Wormholes(Bellman Ford判断负环)
题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...
- UVa 11627 - Slalom 二分. oj错误题目 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 内存池技术(UVa 122 Tree on the level)
内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时, ...
- Binary Search Tree 以及一道 LeetCode 题目
一道LeetCode题目 今天刷一道LeetCode的题目,要求是这样的: Given a binary search tree and the lowest and highest boundari ...
- UVA 558 Wormholes 【SPFA 判负环】
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- UVa 548 Tree (建树+前序后序)
Description You are to determine the value of the leaf node in a given binary tree that is the termi ...
随机推荐
- PHP异常与错误处理机制
先区别一下php中错误 与 异常的概念吧 PHP错误:是属于php程序自身的问题,一般是由非法的语法,环境问题导致的,使得编译器无法通过检查,甚至无法运行的情况.平时遇到的warming.notice ...
- [转]MVC、MVP、MVVM
界面之下:还原真实的 MVC.MVP.MVVM 模式 [日期:2015-10-28] 来源:github.com/livoras 作者:戴嘉华 [字体:大 中 小] 前言 做客户端开发.前端开发 ...
- jquery 监控文本框键盘事件(回车事件),附常用keycode值。
$(function(){ $(".search").keydown(function(event) { ) { //执行操作 } }) ); 完整的 key press 过程分为 ...
- javascript --- Ajax基础
神马是Ajax? Ajax即‘Asynchronous javascript and XML’(异步javascript和XML),也就是所谓的无刷新页面读取技术. http请求 首先要了解http请 ...
- C++调用C#dll类库中的方法(非显性COM)
一般在网上搜C++如何调用C#的函数,出来的结果都是做成COM组件,但是这种方法dll安装麻烦,需要注册COM组件,需要管理员权限,调试麻烦,经常需要重启机器,反正有诸多不便. 然后在看<CLR ...
- SharePoint 2010: Nailing the error "The Security Token Service is unavailable"
http://blogs.technet.com/b/sykhad-msft/archive/2012/02/25/sharepoint-2010-nailing-the-error-quot-the ...
- 浅谈PopupWindow弹出菜单
实现将一个View显示在某一位置,而且是浮于当前窗口 首先要有一个要显示的view的布局,可以是任意View,包括ViewGroup <?xml version="1.0" ...
- Android Studio安装使用图文教程(转)
[开发环境] 物理机版本:Win 7旗舰版(64位) Java SDK版本:jdk1.8.0_20(64位) Android SDK版本:Android 4.4(API 20) Android Stu ...
- CoreAnimation-05-CABasicAnimation
概述 简介 CABasicAnimation是抽象类CAPropertyAnimation的子类,可以直接使用 CABasicAnimation又称基本动画,从fromValue到toValue按照指 ...
- iOS之微博UI实例--拟物化设计(成功了90%)
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...