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 ...
随机推荐
- 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)
matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...
- Linux命令详解之—cat命令
cat命令的功能是连接文件或标准输入并打印,今天就为大家介绍下Linux中的cat命令. 更多Linux命令详情请看:Linux命令速查手册 Linux 的cat命令通常用来显示文件内容,也可以用来将 ...
- ArrayList,Hashtable,List<T>,Dictionary<K,V>
1.ArrayList ArrayList list = new ArrayList(); //for遍历 ; i < list.Count; i++) { SE se=(SE)list[i]; ...
- Windows程序==>>使用ListView控件展示数据
使用ListView控件展示数据 01.ImageList控件 1.了解了解 属性 说明 Images 储存在图像列表中的所有图像 ImageSize 图像列表中图像的大小 Trans ...
- log4j.xml 配置参数属性level使用心得
jdbc.sqlonly 只显示执行的sql语句.info级才可以显示,debug增加显示java源代码位置. jdbc.sqltiming 显示执行的sql语句以及语句执行时间, ...
- 【.net程序破解】实战之标志位破解绕过注册法
今天有时间玩了下一个不错的软件Advanced System Cleaner,可惜要注册 于是想办法给破解了,这是跟之前不同的地方,属于.NET破解教程: 软件地址 - http://www.crsk ...
- javascript Array 方法学习
原生对象Array学习 Array.from() 从类似数组的对象或可迭代的对象返回一个数组 参数列表 arraylike 类似数组的对象或者可以迭代的对象 mapfn(可选) 对对象遍历映 ...
- JY游戏之毁经典《扫雷》
JY游戏之毁经典<扫雷> 这是一个经典的pc端游戏,一定的运气加一点数学常识,讲的是一个速度,这次,我利用js JY库重做了这款游戏,加了三次生命,过关难度,也兼容了移动端的触摸事件. 它 ...
- css百宝箱
关于css百宝箱? 在前端学习中,总会遇到零星的知识点,小技巧,这些知识点小到不至于用一片博客写出来,遇到时网上查询一下或许也能搞定,但不一定能记住,所以这篇博客就用来记录那些散落的知识点,积少成多, ...
- AngularJS 最常用的功能
第一 迭代输出之ng-repeat标签ng-repeat让table ul ol等标签和js里的数组完美结合 1 2 3 4 5 <ul> <li ng-repeat="p ...