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. uvaoj 1081510815 - Andy's First Dictionary(set应用)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&page= ...

  2. cf#514B. Forgery(暴力)

    B. Forgerytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputsta ...

  3. Kotlin的密封(Sealed)类:超强的枚举(KAD 28)

    作者:Antonio Leiva 时间:Jun 27, 2017 原文链接:https://antonioleiva.com/sealed-classes-kotlin/ Kotlin的封装类是Jav ...

  4. selenium,unittest——自动化执行多个py文件脚本并生成报告

    将多个py文件的自动化脚本顺序运行,并生成报告,运行run_all_case后会自动运行文件内所有test开头的py文件并在指定文件夹report生成由脚本时间命名的报告 脚本执行后结果: 生成报告并 ...

  5. CF245H Queries for Number of Palindromes

    题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 时空限制 5000ms,256MB 输入格式 第1行,给出s,s的长度 ...

  6. eos开发指南

    十分钟教你开发EOS智能合约 在CSDN.柏链道捷(PDJ Education).HelloEOS.中关村区块链产业联盟主办的「EOS入门及最新技术解读」专场沙龙上,柏链道捷(PDJ Educatio ...

  7. 从oracle导入hive

    sqoop import --connect jdbc:oracle:thin:@10.39.1.43:1521/rcrm --username bi_query --password ####### ...

  8. POJ 1815 Friendship(最大流最小割の字典序割点集)

    Description In modern society, each person has his own friends. Since all the people are very busy, ...

  9. 福大软工1816:Alpha(5/10)

    Alpha 冲刺 (5/10) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务: 文字/口头描述: 1.忙于复习,本次无成果 展示 ...

  10. java实现几种简单的排序算法

    public class SimpleAri { public static void main(String[] args) { int[] t = {11, 21, 22, 1, 6, 10, 3 ...