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. 「LeetCode」0002-Longest Substring Without Repeating Characters(C++)

    分析 贪心思想.注意更新每次判断的最长不同子串的左区间的时候,它是必须单调增的(有时候会在这里翻车). 代码 关掉流同步能有效提高速度. static const auto io_sync_off = ...

  2. hdu1052Tian Ji -- The Horse Racing(贪心,细节多)

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  3. Python3开启Http服务

    在CMD命令行输入D: 切换到D盘, 然后输入 python -m http.server 8000 开启HTTP服务: 在浏览器地址栏输入 http://localhost:8000/

  4. 第三十九篇 Python异常处理

    一. 什么是异常 异常就是程序运行时发生的错误,在程序出现错误时,则会产生一个异常,若程序没有处理它,则会抛出该异常,程序的运行也随之终止,在python中,错误触发的异常如下 错误分成两种: #语法 ...

  5. 第八模块:算法&设计模式、企业应用 第2章 企业应用工具学习

    第八模块:算法&设计模式.企业应用 第2章 企业应用工具学习

  6. MVC数据的注册及验证简单总结

    一.注解 注解是一种通用机制,可以用来向框架注入元数据,同时,框架不只驱动元数据的验证,还可以在生成显示和编辑模型的HTML标记时使用元数据. 二.验证注册的使用 1.Require:属性为Null或 ...

  7. Siki_Unity_1-9_Unity2D游戏开发_Roguelike拾荒者

    Unity 1-9 Unity2D游戏开发 Roguelike拾荒者 任务1:游戏介绍 Food:相当于血量:每走一步下降1,吃东西可以回复(果子10药水20),被怪物攻击会减少中间的障碍物可以打破, ...

  8. 日历(Calendar)模块

    #usr/bin/python3 #! -*-conding : utf-8 -*- #2018.3.14 """ 日历(Calendar)模块 此模块的函数都是日历相关 ...

  9. 《javascript模式--by Stoyan Stefanov》书摘--函数

    三.函数 1.函数的命名属性 // IE下不支持name属性 var foo = function bar () { // todo }; foo.name; // "bar" 2 ...

  10. 找bug——加分作业

    bug1:while循环中的*des++ =*src++; 不能这么写吧... bug2:maxSize没有定义 暂时看到这么多