UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS)
题意分析
给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小。若有多个,输出叶子节点本身权值小的那个节点。
先递归建树,然后DFS求解。
代码总览
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <vector>
#define nmax 100000
#define INF 0x3f3f3f3f
using namespace std;
int inorder[nmax],postorder[nmax];
int lnode[nmax],rnode[nmax];
int i;
bool read_input()
{
memset(inorder,0,sizeof(inorder));
memset(postorder,0,sizeof(postorder));
string s;
if(!getline(cin,s)) return false;
else{
stringstream ss(s);
i = 0;
int x;
while(ss >> x) inorder[i++] = x;
getline(cin,s);
stringstream sss(s);
i = 0;
while(sss>>x) postorder[i++] = x;
}
return true;
}
// 递归建树
int buildtree(int l1,int r1, int l2, int r2)
{
if(l1>r1) return 0;
int root = postorder[r2];
int p = l1;
while(inorder[p] != root) p++;
int cnt = p-l1;
lnode[root] = buildtree(l1,p-1,l2,l2+cnt-1);
rnode[root] = buildtree(p+1,r1,l2+cnt,r2-1);//
return root;
}
int bestsum,best;
void dfs(int u, int sum)
{
sum+=u;
if(!lnode[u] && !rnode[u])
if(sum<bestsum || (sum == bestsum && u<best)){
best = u;
bestsum = sum;
}
if(lnode[u]) dfs(lnode[u],sum);
if(rnode[u]) dfs(rnode[u],sum);
}
int main()
{
//freopen("in.txt","r",stdin);
while(read_input()){
bestsum = INF;best = INF;
buildtree(0,i-1,0,i-1);
dfs(postorder[i-1],0);
cout<<best<<"\n";
}
return 0;
}
UVA.548 Tree(二叉树 DFS)的更多相关文章
- UVa 548 Tree(二叉树最短路径)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- UVa 548 Tree【二叉树的递归遍历】
题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...
- uva 548 Tree(通过后序,先序重建树+dfs)
难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...
- Tree UVA - 548(二叉树递归遍历)
题目链接:https://vjudge.net/problem/UVA-548 题目大意:给一颗点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序遍历和后序遍历,找一个叶子结点使得它到根 ...
- 【紫书】Tree UVA - 548 静态建树dfs
题意:给你中序后序 求某叶子节点使得从根到该节点权值和最小.若存在多个,输出其权值最小的那个. 题解:先建树,然后暴力dfs/bfs所有路径,取min 技巧:递归传参数,l1,r1,l2,r2, su ...
- UVA - 548 Tree(二叉树的递归遍历)
题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #inc ...
- UVA 548(二叉树重建与遍历)
J - Tree Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Ap ...
- Uva 548 Tree
0.这是一道利用中序遍历和后序遍历确定二叉树的题目,学会建树 关键点理解这段代码 int build(int L1,int R1,int L2,int R2) { //printf("bui ...
- UVa 548 Tree (建树+前序后序)
Description You are to determine the value of the leaf node in a given binary tree that is the termi ...
随机推荐
- CentOS 7.2静默安装Oracle11g
Preface Today I'm gonna export some test data to another server.The source server is Windows ...
- Java多线程之volatile与synchronized比较
可见性: JAVA内存模型: java为了加快程序的运行效率,对一些变量的操作是在寄存器或者CPU缓存上进行的,后面再同步到主存中 看上图,线程在运行的过程中,会从主内存里面去去变量,读到自己的空间内 ...
- 【if控制器】-(某种情况成立就执行的场景)
if 控制器 一般来判断某种特殊情况 成立,就执行. JEXL Expression to evaluate:此处直接填写需要进行判断的表达式即可 表达式支持: == 是否等于,如${__jex ...
- Grid 网格布局
CSS 网格布局(Grid Layout) 是CSS中最强大的布局系统. 这是一个二维系统,这意味着它可以同时处理列和行,不像 flexbox 那样主要是一维系统. 你可以通过将CSS规则应用于父元素 ...
- linux NULL 的定义
#undef NULL #if defined(__cplusplus) #define NULL 0 #else #define NULL ((void *)0) #endif
- java对json文件的操作
第一步:通过FileReader读取json文件第二步:使用BufferReader,先通过I/O读取一定大小的数据缓存到数组中,然后再从数组取出数据.第三步:用一个字符串把每次传来的数据处理后写到新 ...
- nodejs promise深度解析
Promise本质上是一个容器,内部有一个执行函数,当promise对象New出来的时候,内部包裹的函数立即执行. V8引擎会将resolve和projeccted两个函数传递进来,resolved含 ...
- Java学习个人备忘录之接口
abstract class AbsDemo { abstract void show1(); abstract void show2(); } 当一个抽象类中的方法都是抽象的时候,这时可以将该抽象类 ...
- 进程间通信:命名管道FIFO(2)
一.命名管道 如果我们想在不相关的进程之间交换数据,可以用FIFO文件来完成这项工作,它通常也被称为命名管道.命名管道是一种特殊类型的文件,它在文件系统中以文件名的形式存在,但是它的行为却和我们已经见 ...
- (五)final修饰符
final修饰变量 final修饰符一般用于基本数据类型(int,float)或者不可变对象(String).这时候可以看作常量变量. 但是当final作用于可变数据类型时(数组,一般对象),仅仅表示 ...