题目描述:

题目思路:

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)的更多相关文章

  1. Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。

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

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

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

  3. Tree UVA - 548(二叉树递归遍历)

    题目链接:https://vjudge.net/problem/UVA-548 题目大意:给一颗点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序遍历和后序遍历,找一个叶子结点使得它到根 ...

  4. Tree UVA - 548

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

  5. UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

    Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...

  6. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  7. 树(tree)

    树(tree)[题目描述]从前在森林里面有一棵很大的树,树上住着很多小动物.树上有

  8. JS--插件: 树Tree 开发与实现

    日常在Web项目开发时,经常会碰到树形架构数据的显示,从数据库中获取数据,并且显示成树形.为了方便,我们可以写一个javascript的一个跨浏览器树控件,后续可以重复使用.本节分享一个自己开发的JS ...

  9. UVa 548 Tree(二叉树最短路径)

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

随机推荐

  1. HDU 1024 Max Sum Plus Plus(m个子段的最大子段和)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/ ...

  2. Use PerfHUD ES to Do Frame Capture Android Game

    Author: http://www.cnblogs.com/open-coder/p/3898224.html Get Start This is short tutorial about how ...

  3. 19-3-5Python中列表、元组、以及range

    一.列表: 为什么要学列表? 因为字符串存在缺点: 1)      只能存储少量的数据. 2)      数据类型无论索引.切片 获取的都是字符串类型,类型过于单一,转化成它原来的类型还需要进一步转换 ...

  4. 08.nextcloud搭建

    由于公司用的nfs文件共享系统满足不了权限需求,测试nextcloud是否符合要求 参考博客: https://www.cnblogs.com/davidz/articles/9686716.html ...

  5. Android 微信页面刷新问题

    今天测试妹纸提了个bug,Android手机用微信打开测试页面,刷新功能无效.因为开发时懒,只验证了Ios手机无异常,没有注意打安卓这个问题. 我是直接用的window.location.reload ...

  6. mysql8.0新增用户及密码加密规则修改

    MySQL8.0已经发布GA版,当前最新GA版本为8.0.12.虽然相对于之前版本,MySQL8.0没有加入新元素,但是,经过代码重构,MySQL8.0的优化器更加强大,同时也有一些新特性,如支持索引 ...

  7. day 25 模块与包

    一.模块   模块就是一个包含了python定义和申明的文件,文件名就是模块的名字加上.py的后缀/ 模块的分类:     1.使用python编写的py文件     2.已被编译位共享库或者DLL或 ...

  8. 使用 win10 的库来组织自己的同类文件

    库相当于虚拟目录,可以把不同的文件夹包含起来. 找起东西来不用东奔西跑了...

  9. ubuntu下的python请求库的安装——Selenium,ChromeDriver,GeckoDriver,PhantomJS,aiohttp

    Selenium安装: pip3 install selenium ChromeDriver安装: 在这链接下载对应版本:https://chromedriver.storage.googleapis ...

  10. OpenWrt超时检测

    参考http://www.right.com.cn/forum/thread-261702-1-1.html vim /home/ihid/chaos_calmer/feeds/luci/module ...