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 ...
随机推荐
- Winform窗体实现简单的二维码生成和保存
二维码的生成需要用到二维码生成的类库,ThoughtWorks.QRCode.dll 步骤: 第一步:下载二维码生成类库,ThoughtWorks.QRCode.dll 第二步:新建winform项目 ...
- 序列中找子序列的dp
题目网址: http://codeforces.com/problemset/problem/414/B Description Mashmokh's boss, Bimokh, didn't lik ...
- No.011:Container With Most Water
问题: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- C程序(3)
- PHP学习笔记:keditor的使用
keditor时一个免费的开源编辑器,很多公司在使用(百度编辑器也不错).最近为了做一个客户信息管理系统,在发送邮件模块用到这个编辑器,也算学习一下新的东西. 第一步:下载编辑器 到它的官网下载:ht ...
- [js开源组件开发]js手机端浮层控件,并有多种弹出小提示,兼容pc端浏览器
js dialog组件,包含alert和confirm的实现 本组件所有的资源均在github上可以查看源代码 GitHub 本dialog的组件的例子请在这里查看 demo dialog js di ...
- CRM 2013 Reporting Extensions for SSRS 安装及问题解决
说明一下 Reporting Extensions for SSRS 安装过程. 安装目录在安装目录下 SrsDataConnector 下.如果是CRM 2013安装中运行,可以跳过此步. 此外在说 ...
- SharePoint Iframe 报错“此内容不能显示在一个框架中”
问题描述 我们SharePoint站点用Excel Service发布的Excel,需要Iframe到其他系统中,但是,Iframe的时候发现报错“此内容不能显示在一个框架中”. 后来,尝试在其他系统 ...
- 404 & 401 Errors with the App Management Service
from:http://blogs.technet.com/b/sharepoint_-_inside_the_lines/archive/2013/06/23/404-amp-401-errors- ...
- Windows 下Apace tomcat
java JDK安装: 1. 官方www.oracle.com 下载jdk 2. 环境变量配置 (1)新建->变量名:JAVA_HOME变量值:C:\Program Files (x86)\Ja ...