UVA 548(二进制重建和遍历)
System Crawler (2014-05-16)
Description

Tree |
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
Miguel A. Revilla
1999-01-11
题意:给你二叉树的中序与后序。求从根到叶子全部值之和最小的叶子的值。
依据中序和后序递归建树,直接遍历就可以。
#include<stdio.h>
#include<cstring>
int a[10001],b[10001];
int M,v;
struct tree
{
int date;
tree *l,*r;
tree()
{
date=0;
l=r=NULL;
}
};
tree* built(int *A,int *B,int n)
{
if(!n)return NULL;
tree *now=new tree;
int i=0;
for(i=0;i<n;i++)if(A[i]==B[0])break;//找到根在中序遍历中的位置
if(i>0)now->l=built(A,B+n-i,i);//递归建立左子树
if(i<n-1)now->r=built(A+i+1,B+1,n-i-1);//递归建立右子树
now->date=B[0];
return now;
}
void del(tree *p)
{
if(!p)return;
if(p->l)del(p->l);
if(p->r)del(p->r);
delete p;
p=NULL;
}
void dfs(tree *Root,int sum)
{
if(Root==NULL)return ;
//printf("%d ",Root->date);
if(Root->l==NULL&&Root->r==NULL)
{
if(sum+Root->date<M)
{
M=sum+Root->date;
v=Root->date;
}
}
dfs(Root->l,sum+Root->date);
dfs(Root->r,sum+Root->date);
}
int main()
{
char ch;
tree *root;
//freopen("in.txt","r",stdin);
while(~scanf("%d",&a[0]))
{
root=NULL;
int n=1;
M=100000001;
v=0;
while((ch=getchar())!='\n')
{
scanf("%d",a+n);++n;
}
for(int i=n-1;i>=0;i--)
{
scanf("%d",b+i);
}
root=built(a,b,n);
dfs(root,0);
printf("%d\n",v);
del(root);
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
UVA 548(二进制重建和遍历)的更多相关文章
- UVa 548 (二叉树的递归遍历) Tree
题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍 ...
- Tree UVA - 548(二叉树递归遍历)
题目链接:https://vjudge.net/problem/UVA-548 题目大意:给一颗点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序遍历和后序遍历,找一个叶子结点使得它到根 ...
- UVA - 548 根据中序遍历和后序遍历建二叉树(关于三种遍历二叉树)
题意: 同时给两个序列,分别是二叉树的中序遍历和后序遍历,求出根节点到叶子结点路径上的权值最小和 的那个 叶子节点的值,若有多个最小权值,则输出最小叶子结点的和. 想法: 一开始想着建树,但是没有这样 ...
- Uva 548 二叉树的递归遍历lrj 白书p155
直接上代码... (另外也可以在递归的时候统计最优解,不过程序稍微复杂一点) #include <iostream> #include <string> #include &l ...
- UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)
Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...
- Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- UVA 548(二叉树重建与遍历)
J - Tree Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Ap ...
- UVa 548 Tree【二叉树的递归遍历】
题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...
随机推荐
- C++ 指针(不论什么一个指针本身的类型都是unsigned long int型)
1.指针数组: 即 数组的元素是指针型; 例:int*pa[2]; 明明是一维的指针数组.竟当作二维数组用. [cpp] view plain copy //利用指针数组存放单位矩阵 #include ...
- angular项目中各个文件的作用
原文地址 https://www.jianshu.com/p/176ea79a7101 大纲 1.对angular项目中的一些文件的概述 2.对其中一些文件的详细描述 2.1.package.json ...
- 【搜索引擎Jediael开发笔记】v0.1完整代码 2014-05-26 15:17 463人阅读 评论(0) 收藏
详细代码请见 E:\Project\[重要]归档代码\SearchEngine归档代码 或 https://code.csdn.net/jediael_lu/jediael/tree/10991c83 ...
- 小雷FansUnion:我有了第一个付费客户(第一个徒弟)
很高兴地告诉大家一个振奋人心的消息,我刚刚拥有了第一个付费客户. 第一个付费客户是山东青岛的一个上班族,有2年.Net经验,今年转Java开发.对我比较信任,在我的建议下,选择了"拜师学艺& ...
- html5-8 如何控制html5中的视频标签和音频标签
html5-8 如何控制html5中的视频标签和音频标签 一.总结 一句话总结:找到视频或者音频的element对象,然后查手册看对应的方法或者属性就可以,里面有控制的. 1.如何控制html5中的视 ...
- 【Linux】Linux下配置apache - 安装文件夹具体解释
一,apache安装路径解释 默认安装路径 /var/apache2 # /etc/apache2/ # |-- apache2.conf # | `-- ports.conf # |-- mo ...
- 授人玫瑰 手留余香 --纪念python3.2.3官方文档翻译结束
当你点击看到这篇文章的时候.你已经得到了祝福. 一个来自夜深人静的码农,在2014年5月19号的01:18分.默默为你献上祝福. 希望你.我和他,每个在IT行业中奋斗的人.能找到属于自己一片天空. 在 ...
- js进阶 10-7 简单的伪类选择器可以干什么
js进阶 10-7 简单的伪类选择器可以干什么 一.总结 一句话总结:伪类选择器是冒号. 1.学而不用,有什么用? 多用啊,在项目中多用 2.简单的伪类选择器可以干什么? 除某元素以外,某元素的一切索 ...
- hdu 4865 dp
Peter's Hobby Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- 01_Git的安装和简单使用(命令行模式+图形化模式)
刚开始用git的小白适用,参考链接:http://www.cnblogs.com/qijunjun/p/7137207.html 实际项目开发中,我们经常会用一些版本控制器来托管自己的代码,今天 ...