树(Tree,UVA 548)
题目描述:

题目思路:
1.使用数组建树 //递归
2.理解后序遍历和中序遍历,建立左右子树
3.dfs深度搜索找出权重最小的路径
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std; const int maxv = + ;
int in_order[maxv], post_order[maxv], lch[maxv], rch[maxv];
int i; bool readlist(int* t){ //读入输入
string line;
if(!getline(cin,line)) return false ;
stringstream ss(line);
i = ;
int x;
while(ss >> x) t[i++] = x;
return i > ;
} int buildtree(int L1, int R1, int L2, int R2){//将读入的两行建树
if(L1 > R1) return ; // 空树
int root = post_order[R2] ;//后序遍历最后一个
int p = L1;
while(in_order[p] != root) p++;
int cnt = p-L1; // 左子树的结点个数
lch[root] = buildtree(L1, p-, L2, L2+cnt-);
rch[root] = buildtree(p+, R1, L2+cnt, R2-);
return root ;
} int best, best_sum; // 目前为止的最优解和对应的权和 void dfs(int u, int sum){//深度搜索
sum += u;
if(!lch[u] && !rch[u]) { // 叶子
if(sum < best_sum || (sum == best_sum && u < best))
{ best = u; best_sum = sum; }
}
if(lch[u]) dfs(lch[u], sum);
if(rch[u]) dfs(rch[u], sum);
} int main(int argc, char *argv[])
{
while(readlist(in_order)) {
readlist(post_order);
buildtree(, i-, , i-);
best_sum = ;
dfs(post_order[i-], );
cout << best << "\n";
}
return ;
}
树(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 ...
- 【紫书】Tree UVA - 548 静态建树dfs
题意:给你中序后序 求某叶子节点使得从根到该节点权值和最小.若存在多个,输出其权值最小的那个. 题解:先建树,然后暴力dfs/bfs所有路径,取min 技巧:递归传参数,l1,r1,l2,r2, su ...
- Tree UVA - 548(二叉树递归遍历)
题目链接:https://vjudge.net/problem/UVA-548 题目大意:给一颗点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序遍历和后序遍历,找一个叶子结点使得它到根 ...
- Tree UVA - 548
You are to determine the value of the leaf node in a given binary tree that is the terminal node o ...
- UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)
Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- 树(tree)
树(tree)[题目描述]从前在森林里面有一棵很大的树,树上住着很多小动物.树上有
- JS--插件: 树Tree 开发与实现
日常在Web项目开发时,经常会碰到树形架构数据的显示,从数据库中获取数据,并且显示成树形.为了方便,我们可以写一个javascript的一个跨浏览器树控件,后续可以重复使用.本节分享一个自己开发的JS ...
- UVa 548 Tree(二叉树最短路径)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
随机推荐
- 【题解】洛谷P1879 [USACO06NOV] Corn Fields(状压DP)
洛谷P1879:https://www.luogu.org/problemnew/show/P1879 思路 把题目翻译成人话 在n*m的棋盘 每个格子不是0就是1 1表示可以种 0表示不能种 相邻的 ...
- win7系统安装VS2013后,连不上远程sqlserver数据库解决办法
刚搬了地方,顺便把电脑重做了系统,把sql2012和vs2013装好,怎么弄也连不上远程的数据库了,用程序连IIS直接死掉,用ssms连也是直接失去响应,开始以为是网线端口被运营商封杀了,最后发现不是 ...
- Gradle Goodness: Set Java Compiler Encoding
If we want to set an explicit encoding for the Java compiler in Gradle we can use the options.encodi ...
- Oracle恢复误删数据
1.先查出被删除的时间点: select * from flashback_transaction_query where table_name='表名'; 2.根据时间点恢复数据: insert i ...
- MySQL中的事件/定时任务
转载自:http://www.cnblogs.com/chenpi/p/5137310.html 什么是事件 一组SQL集,用来执行定时任务,跟触发器很像,都是被动执行的,事件是因为时间到了触发执行, ...
- Javascript混淆与解混淆的那些事儿
像软件加密与解密一样,javascript的混淆与解混淆同属于同一个范畴.道高一尺,魔高一丈.没有永恒的黑,也没有永恒的白.一切都是资本市场驱动行为,现在都流行你能为人解决什么问题,这个概念.那么市场 ...
- awk命令用法
awk:把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理,是一个强大的文本分析工具,在对数据分析并生成报告时很有优势. awk有3个不同版本: awk.nawk和gawk, ...
- Linux进程地址空间 && 进程内存布局[转]
一 进程空间分布概述 对于一个进程,其空间分布如下图所示: 程序段(Text):程序代码在内存中的映射,存放函数体的二进制代码. 初始化过的数据(Data):在程序运行初已经对变量进行初始 ...
- linux系统常用命令统计及shell特殊字符
shell 特殊字符:1.通配符2.管道 1.通配符 1.1星号(*):匹配任意长度 1.2问号(?):匹配一个长度的字符 1.3方括号([......]):匹配其中指定的字符 1.4方括号([-]) ...
- Spring总结以及在面试中的一些问题
Spring总结以及在面试中的一些问题. 1.谈谈你对spring IOC和DI的理解,它们有什么区别? IoC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建Use ...