【暑假】[基本数据结构]根据in_order与post_order构树
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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构树的更多相关文章
- Uva 548 Tree
0.这是一道利用中序遍历和后序遍历确定二叉树的题目,学会建树 关键点理解这段代码 int build(int L1,int R1,int L2,int R2) { //printf("bui ...
- UVa 548 (二叉树的递归遍历) Tree
题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍 ...
- F - Tree
Description You are to determine the value of the leaf node in a given binary tree that is the termi ...
- 例题6-8 Tree Uva548
这道题我一直尝试用scanf来进行输入,不过一直没有成功,因此先搁置一下,以后积累些知识再进行尝试. 这道题有两种解决方案: 即先建树,再遍历和边建树边遍历.这两种方案经过实践证实效率相差不太多.应该 ...
- UVa 548 Tree【二叉树的递归遍历】
题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...
- Uva 548 二叉树的递归遍历lrj 白书p155
直接上代码... (另外也可以在递归的时候统计最优解,不过程序稍微复杂一点) #include <iostream> #include <string> #include &l ...
- 二叉树的递归遍历 Tree UVa548
题意:给一棵点带权的二叉树的中序和后序遍历,找一个叶子使得他到根的路径上的权值的和最小,如果多解,那该叶子本身的权值应该最小 解题思路:1.用getline()输入整行字符,然后用stringstre ...
- UVA548-Tree(二叉树数组表示)
Problem UVA548-Tree Accept: 2287 Submit: 13947 Time Limit: 3000 mSec Problem Description You are to ...
- 【紫书】Tree UVA - 548 静态建树dfs
题意:给你中序后序 求某叶子节点使得从根到该节点权值和最小.若存在多个,输出其权值最小的那个. 题解:先建树,然后暴力dfs/bfs所有路径,取min 技巧:递归传参数,l1,r1,l2,r2, su ...
随机推荐
- 表连接到底咋回事,就是产生中间结果啊!用于给select/insert等操作用
1.表连接到底咋回事,就是产生中间结果啊!用于给select/insert等操作用啊. 2.表连接产生的结果用于select/insert用 3.表连接产生的结果用于select/insert用 比如 ...
- ubuntu 下搭建vsftp
1. 安装:sudo apt-get install vsftpd 2. 我的目的是建立个ftp,专门的账户访问,账户不可以登陆.不允许匿名登陆 3. 更改配置文件/etc/vsftpd.conf l ...
- GitHub最全的前端资源汇总仓库(包括前端学习、开发资源、求职面试等)
在GitHub上收集的最全的前端资源汇总(包括前端学习.前端开发资源.前端求职面试等) 个人结合github上各位大神分享的资源进行了简单的汇总整理,每一个条目下面都有丰富的资料,是前端学习.工作的好 ...
- django定期执行任务
要在django项目中定期执行任务,比如每天一定的时间点抓取数据,刷新数据库等,可以参考stackoverflow的方法,先编写一个manage.py命令,然后使用crontab来定时执行这个命令. ...
- SQLserver临时表
IF EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME='#temp') DROP TABLE #tempGOSELECT ID,XM,ADDDW INTO #t ...
- cmd修改系统时间
time 11:15:00 修改时间 date 2015/11/25 修改日期
- ConcurrentDictionary和Dictionary
http://stackoverflow.com/questions/6739193/is-the-concurrentdictionary-thread-safe-to-the-point-that ...
- Linux下检查是否安装过某软件包
1.rpm包安装的,可以用 rpm -qa 看到,如果要查找某软件包是否安装,用 rpm -qa | grep "软件或者包的名字" 2.以deb包安装的,可以用 dpkg -l ...
- python写的第一个简单小游戏-猜数字
#Filename:game1.py guess=10 running=True while running: try: answer=int(raw_input('Guess what i thin ...
- JVM 问题排查常用工具
一. jmap // 打印jvm的堆状况,主要是年轻代和老年代信息 jmap -heap <pid> 如: Heap Configuration: MinHeapFreeRatio = M ...