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 ...
随机推荐
- [PE结构分析] 9.导出表 IMAGE_EXPORT_DIRECTORY
typedef struct _IMAGE_EXPORT_DIRECTORY { DWORD Characteristics; // 未使用,总为0 DWORD TimeDateStamp; // 文 ...
- jquery自定义插件——window的实现
本例子实现弹窗的效果: 1.jquery.show.js /* * 开发者:lzugis * 开发时间:2014年6月10日 * 实现功能:点击在鼠标位置显示div * 版本序号:1.0 */ (fu ...
- ASP.NET WebAPI 10 Action的选择(二)
在本系列的第二篇简要的讲述了Action的选择条件本篇深入讲述一下Action选择的过程在上一篇中我们已经讲到了Controller的激活过程中已经说到了设置Controller的Controller ...
- 硅谷新闻3--使用Android系统自带的API解析json数据
NewsCenterPagerBean2 bean2 = new NewsCenterPagerBean2(); try { JSONObject object = new JSONObject(js ...
- Android5.0新特性——阴影和剪裁(shadow)
阴影和剪裁 View的z属性 Material Design建议为了凸显布局的层次,建议使用阴影效果,并且Android L为了简化大家的工作,对View进行了扩展,能使大家非常方便的创建阴影效果: ...
- SharePoint 自定义WebPart之间的连接
1.创建SharePoint解决方案,添加两个WebPart分别用来发送和接收: 2.发送值的WebPart需要继承自IWebPartField(当然,根据需要还可以选择IWebPartField,I ...
- SharePoint 使用代码为页面添加WebPart
传统的SharePoint实施中,我们通常会创建SharePoint页面,然后添加webpartzone,而后在上面添加webpart:但是有些情况下,也要求我们使用代码,将webpart添加到相应w ...
- 导出你的GAC Assembly中的DLLS -- 金大昊(jindahao)
导出你的GAC Assembly中的DLLS 方法1: CMD命令中,进入C:\windows\assembly,然后XCOPY GAC_MSIL c:\temp /E 这样就得到了dlls了,以 ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q127-Q130)
Question 127You create a custom list named Products.You need to perform a Representational State Tra ...
- UWP开发-重新理解MVVM
MVVM是一个比较热门的开发框架,尽管已经出现很久了,仍然比较受欢迎.MVVM框架包括: M:Model:Model指的是数据模型,例如你要在页面展示联系人信息,那么Model就是联系人的模型,包括联 ...