题目描述:

题目思路:

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. 转:system.Security.Cryptography C# 加密和解密

    以下文转自: http://www.360doc.com/content/13/0122/05/19147_261678471.shtml 总结:注册的时候经过MD5加密存进数据库,在登录的时候需要先 ...

  2. Flask—10-项目部署(02)

    项目部署 WEB工作原理 客户端(chrom) <=> WEB服务器(nginx) <=> WSGI(uWSGI) <=> Python(Flask) <=& ...

  3. c++11线程创建的三种方法

    一.用一个初始函数创建一个线程 直接看代码:注意c++在运行一个可执行程序的时候(创建了一个进程),会自动的创建一个主线程,这个主线程和进程同生共死,主线程结束,进程也就结束了. #include & ...

  4. 复习宝典之Redis

    查看更多宝典,请点击<金三银四,你的专属面试宝典> 第八章:Redis Redis是一个key-value的nosql数据库.先存到内存中,会根据一定的策略持久化到磁盘,即使断电也不会丢失 ...

  5. VMware10安装CentOS7

    先去网上下载一个VMware的破解版或者激活版,安装配置这里就不介绍了自行下载安装,基本过程相当于windows下安装个软件而已. CentOS7镜像下载就下阿里云站点的这是链接 http://mir ...

  6. 编译升级至openssh7.6

    1.概述 目的:下载源码包(https://openbsd.hk/pub/OpenBSD/OpenSSH/portable/openssh-7.6p1.tar.gz),编译升级为openssh为7.6 ...

  7. Webstorm新建vue类型文件设置

    今天安装了Node.js,配置了vue需要的框架,发现原有的wenstorm新建文件的时候没有vue文件选项,因此,学习了一下webstorm如何配置创建vue文件 具体过程如下: 第一步,打开web ...

  8. vscode vue 项目保存运行lint进行代码修正

    { "editor.tabSize": 2, "files.associations": { "*.vue": "vue" ...

  9. GitLab 基本操作

    登录 在浏览其中输入http://192.168.3.11:8888 如图1登录界面.   图1 注:第一次新增用户,会发送修改密码链接到用户的邮箱中,用户会收到如图2邮件. 图2 2. 修改密码 点 ...

  10. 利用谷歌浏览器断点调试js反向解析,解密

    目标网站:https://www.aqistudy.cn/html/city_detail.html 点击按钮才会去后台请求数据, 第一步:将click打开, 第二步:找个后台请求数据的url   h ...