树(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 ...
随机推荐
- Mysql 基本语句 + 高级查询
MySQL执行SQL脚本文件的命令: 从cmd进入mysql命令行模式: mysql> -uroot –prootpassword –Ddatabasename 如果是我本地的数据库,就相应修改 ...
- 在vue-cli + webpack 项目中使用sass
1.准备工作: 由于npm的服务器在国外,网速慢而且安装容易失败,建议在安装之前,先安装国内的镜像,比如淘宝镜像 npm install -g cnpm --registry=https://regi ...
- #leetcode刷题之路8-字符串转换整数 (atoi)
请你来实现一个 atoi 函数,使其能将字符串转换成整数.首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止.当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面 ...
- 附件上传——mysql blob类型的数据(springboot)1
作为一个初出茅庐的菜鸟,这几天做了一下附件的上传与下载,附件文件存储在mysql中,数据类型为blob.在此做一下总结.望指正. 一.先总结附件的上传.(实质是将文件传到controller,后处理成 ...
- jQuery.qrcode 生成二维码,并使用 jszip、FileSaver 下载 zip 压缩包至本地。
生成二维码 引用 jquery.qrcode.js :连接:https://files.cnblogs.com/files/kitty-blog/jquery.qrcode.js .https:// ...
- 利用ascii码生成26个英文字母
<script> let a = ""; for (var i = 65; i < 91; i++) { a += String.fromCharCode(i); ...
- 支付宝H5、APP支付服务端的区别(php)
php支付宝H5和APP支付1.准备工作需要前往 蚂蚁金服开放平台申请https://openhome.alipay.com/developmentDocument.htm 2.大致流程1.用户添加商 ...
- day 15 装饰器
装饰器(重点,难点) 开闭原则: 对功能的扩展开放 对代码的修改是封闭的 在目标函数前和后插入一段新的代码.不改变原来的代码 通用装饰器写法: # 存在的 ...
- Linux-3.5-Exynos4412驱动分层分离
linux-3.5/Documentation/driver-model/bus.txt 先写一个简单的例子,是为了给学习platform做准备. dev.h #ifndef JASON_DEV_H_ ...
- [STM32F4][关于看门狗的那些事]
STM32(stm32f4XX系列)看门狗的总结: 1. 具有两个看门狗外设(独立和窗口)均可用于检测并解决由软件错误导致的故障:当计数器达到给定的超时值时,触发一个中断(仅适用于窗口看门狗)或产生一 ...