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

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

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

  2. UVa 548 Tree【二叉树的递归遍历】

    题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...

  3. uva 548 Tree(通过后序,先序重建树+dfs)

    难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...

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

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

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

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

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

    题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #inc ...

  7. UVA 548(二叉树重建与遍历)

    J - Tree Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Ap ...

  8. Uva 548 Tree

    0.这是一道利用中序遍历和后序遍历确定二叉树的题目,学会建树 关键点理解这段代码 int build(int L1,int R1,int L2,int R2) { //printf("bui ...

  9. UVa 548 Tree (建树+前序后序)

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

随机推荐

  1. OSG-交互

    本文转至http://www.cnblogs.com/shapherd/archive/2010/08/10/osg.html 作者写的比较好,再次收藏,希望更多的人可以看到这个文章 互联网是是一个相 ...

  2. 怎样用Eclipse将Java源代码生成可执行文件[转]

    eclipse将java源代码生成jar可执行文件 用eclipse做了一个web项目的自动化测试,自己用的时候倒是很方便,打开eclipse直接运行即可,但是分享给其他小伙伴用的时候就不太方便,希望 ...

  3. go通过第三方库 mahonia gbk 转utf8

    go get github.com/axgle/mahonia dec := mahonia.NewDecoder("GBK")ret:=dec.ConvertString(res ...

  4. lintcode671 循环单词

    循环单词   The words are same rotate words if rotate the word to the right by loop, and get another. Cou ...

  5. (原) MaterialEditor部- UmateriaEditor中 Node编译过程和使用(1)

    @author: 白袍小道 转载说明原处 插件同步在GITHUB: DaoZhang_XDZ     最后YY需求(手滑) 1.在理清楚基础套路和细节后,自定义纹理资源,并加入到现有UE材质系统 2. ...

  6. 利用人脸特征提取DeepID--解读世纪晟人脸识别

    概述:DeepID的目标是人脸验证(判断两张图片是否是一个人),同时衍生出人脸识别(多次人脸验证). DeepID采用增大数据集的方法: 增加新的数据,celebFaces(87628张图片,5436 ...

  7. 基于Kubernetes(k8s)的RabbitMQ 集群

    目前,有很多种基于Kubernetes搭建RabbitMQ集群的解决方案.今天笔者今天将要讨论我们在Fuel CCP项目当中所采用的方式.这种方式加以转变也适用于搭建RabbitMQ集群的一般方法.所 ...

  8. HADOOP docker(一):安装hadoop实验集群(略操蛋)

    一.环境准备 1.1.机器规划 主机名    别名    IP     角色 9321a27a2b91 hadoop1 172.17.0.10 NN1 ZK RM 7c3a3c9cd595 hadoo ...

  9. POJ 2986 A Triangle and a Circle(三角形和圆形求交)

    Description Given one triangle and one circle in the plane. Your task is to calculate the common are ...

  10. sql 至少含有

    查询Score表中至少有5名学生选修的并以3开头的课程的平均分数: select avg(degree),cnofrom scorewhere cno like '3%'group by cnohav ...