Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

Description

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 ------------------------------------------------------------------------------------------------------------------------------------------------------- 算法:pre_order|post_order寻找根节点,in_order判断左右子树,递归处理。DFS to find_ans
 #include<iostream>
#include<cstdio>
#include<sstream>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; const int maxn = + ,INF=<<;
int in_order[maxn],post_order[maxn],lch[maxn],rch[maxn];
int n,best,ans,root; int read_list(int* a){
string line;
if(!getline(cin,line)) return false;
stringstream ss(line);
n=;
int x;
while(ss>>x) a[n++]=x;
return n>;
} int build_tree(int L1,int R1,int L2,int R2){
if(R1<L1) return ;
int root=post_order[R2];
int p=L1;
while(in_order[p] != root) p++;
int cnt=p-L1;
lch[root]=build_tree(L1,p-,L2,L2+cnt-);
rch[root]=build_tree(p+,R1,L2+cnt,R2-);
return root;
} void dfs(int u,int cnt){
cnt += u;
if(cnt>ans) return;
if(!lch[u] && !rch[u])
if(cnt<ans || (cnt==ans && best<u)){ ans=cnt; best=u;}
if(lch[u]) dfs(lch[u],cnt);
if(rch[u]) dfs(rch[u],cnt);
} int main(){
while(read_list(in_order)){
read_list(post_order);
build_tree(,n-,,n-);
ans=INF;
dfs(post_order[n-],);
cout<<best<<endl;
}
return ;
}

【暑假】[基本数据结构]根据in_order与post_order构树的更多相关文章

  1. Uva 548 Tree

    0.这是一道利用中序遍历和后序遍历确定二叉树的题目,学会建树 关键点理解这段代码 int build(int L1,int R1,int L2,int R2) { //printf("bui ...

  2. UVa 548 (二叉树的递归遍历) Tree

    题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍 ...

  3. F - Tree

    Description You are to determine the value of the leaf node in a given binary tree that is the termi ...

  4. 例题6-8 Tree Uva548

    这道题我一直尝试用scanf来进行输入,不过一直没有成功,因此先搁置一下,以后积累些知识再进行尝试. 这道题有两种解决方案: 即先建树,再遍历和边建树边遍历.这两种方案经过实践证实效率相差不太多.应该 ...

  5. UVa 548 Tree【二叉树的递归遍历】

    题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...

  6. Uva 548 二叉树的递归遍历lrj 白书p155

    直接上代码... (另外也可以在递归的时候统计最优解,不过程序稍微复杂一点) #include <iostream> #include <string> #include &l ...

  7. 二叉树的递归遍历 Tree UVa548

    题意:给一棵点带权的二叉树的中序和后序遍历,找一个叶子使得他到根的路径上的权值的和最小,如果多解,那该叶子本身的权值应该最小 解题思路:1.用getline()输入整行字符,然后用stringstre ...

  8. UVA548-Tree(二叉树数组表示)

    Problem UVA548-Tree Accept: 2287  Submit: 13947 Time Limit: 3000 mSec Problem Description You are to ...

  9. 【紫书】Tree UVA - 548 静态建树dfs

    题意:给你中序后序 求某叶子节点使得从根到该节点权值和最小.若存在多个,输出其权值最小的那个. 题解:先建树,然后暴力dfs/bfs所有路径,取min 技巧:递归传参数,l1,r1,l2,r2, su ...

随机推荐

  1. 分布式内存对象缓存系统Memcached-Linux下使用

    Linux下Memcached的使用 1.       安装文件下载 1.1下载memcached服务器端安装文件 版本: memcached-1.4.2.tar.gz 下载地址:http://www ...

  2. mysql字段的适当冗余有利于提高查询速度

    CREATE TABLE `comment` (  `c_id` int(11) NOT NULL auto_increment COMMENT '评论ID',  `u_id` int(11) NOT ...

  3. html5--等待加载效果

    <!DOCTYPE HTML> <html lang="zh-cmn-Hans"> <head> <meta charset=" ...

  4. selvert的过滤器filter处理中文乱码

    注意问题:在学习用selvert的过滤器filter处理中文乱码时,在filter配置初始化时用了utf-8处理中文乱码,而在提交的jsp页面中却用了gbk.虽然两种都可以出来中文乱码,但是却造成了处 ...

  5. Oracle Create DBLink

    DROP PUBLIC DATABASE LINK ORA11G_DBLINK; CREATE   PUBLIC   DATABASE LINK ORA11G_DBLINKCONNECT TO SYS ...

  6. [原]用WebBrowser组件模拟人工运行搜索引擎自动点击搜索结果的实验

    本代码只是业余时间无聊写着试试,用WebBrowser组件模拟人工运行搜索引擎自动点击搜索结果的实验 这是网络中盛传的提高搜索引擎点击率的一种方式,当然属于作弊,不推荐各位使用.另外这种方式的性能不佳 ...

  7. POJ 2752 (KMP 所有可能长度的前缀后缀) Seek the Name, Seek the Fame

    题意: 求一个字符串的相同前缀后缀的所有可能的长度,这里该字符串其本身也算自己的前缀和后缀. 分析: 我们知道next数组的性质是,该字符之前的字符串的最大相同前缀后缀. 既然知道了最大的,即next ...

  8. PHP适合做大型网站吗?

    1. 对递归的不良支持 递归是一种函数调用自身的机制.这是一种强大的特性可以把某些复杂的东西变得很简单.有一个使用递归的例子是快速排序(quicksort).不幸的是,PHP并不擅长递归.Zeev,一 ...

  9. javascript OOP编辑思想的一个实践参考

    <html> <style type="text/css"> .current { background-color: red; } .dv { backg ...

  10. 在linux下实现用ffmpeg把YUV420帧保存成图片

    在网上搜了很久相关的问题,但是好像没有一个在linux下跑得比较完整的例子,不过经过自己一番搜索和总结,终于做出来了,哈哈,看下面的代码吧. 这个例子可以保存成bmp或者jpeg格式的图片. 下面的结 ...