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. iOS开发--浅谈CocoaAsyncSocket编程

    Socket就是一种特殊的文件.它是一个连接了两个用户的文件,任何一个用户向Socket里写数据,另一个用户都能看得到,不管这两个用户分布在世界上相距多么遥远的角落,感觉就像坐在一起传纸条一样. 这么 ...

  2. Error building Player: Win32Exception: ApplicationName=‘xxxxxxxxxxxxxxxxxx//sdk\tools\zipalign.exe' , CommandLine='4 的解决办法

    更新了安卓SDK后,有时候Unity编译失失败,报错类似 Error building Player: Win32Exception: ApplicationName='D:/Program File ...

  3. hadoop拾遗(五)---- mapreduce 输出到多个文件 / 文件夹

    今天要把HBase中的部分数据转移到HDFS上,想根据时间戳来自动输出到以时间戳来命名的每个文件夹下.虽然以前也做过相似工作,但有些细节还是忘记了,所以这次写个随笔记录一下. package com. ...

  4. OSI七层协议

  5. 面试题_31_to_47_JVM 底层与GC(Garbage Collection)的面试问题

    31)64 位 JVM 中,int 的长度是多数?Java 中,int 类型变量的长度是一个固定值,与平台无关,都是 32 位.意思就是说,在 32 位 和 64 位 的Java 虚拟机中,int 类 ...

  6. tranform-scale 缩小元素,移上去文字抖动

    元素缩小后,鼠标移上去之后文字会出现抖动, -webkit-transform:scale(0.5); 修复代码如下: *{ -webkit-backface-visibility: hidden; ...

  7. 设置MySQL主从同步

    1. 配置主服务器 1.1 编辑my.cnf文件,配置主服务器ID. [mysqld] log-bin=mysql-bin server-id=1relay-log = relay-bin relay ...

  8. adb shell settings ....

    Android4.2的源码android-17\com\android\commands目录下较之前的版本多了一个settings命令,查看其中的SettingsCmd.java文件,末尾有命令的帮助 ...

  9. java实现DES算法

    import java.util.UUID; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypt ...

  10. ASP.NET MVC 学习8、Controller中的Detail和Delete方法

    参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and ...