题意:给你中序后序 求某叶子节点使得从根到该节点权值和最小。若存在多个,输出其权值最小的那个。

题解:先建树,然后暴力dfs/bfs所有路径,取min

技巧:递归传参数,l1,r1,l2,r2, sum,root,

代码:

#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include<stdio.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<iostream>
#include<string.h>
#include<queue>
#include<string>
#include<sstream>
using namespace std;
const int maxn = 1e4+;
string line; int n;
int a[maxn],in_order[maxn],post_order[maxn];
int lch[maxn], rch[maxn];
int mn,best;
bool read_list(int *a) {
if (!getline(cin, line)) return false;
stringstream ss(line);
n = ;
int x;
while (ss >> x)a[n++] = x;
return n > ;
}
int build(int l1, int r1, int l2, int r2) {//将中序l1,r1 将后序l2,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] = build(l1, p - , l2, l2 + cnt - );
rch[root] = build(p + , r1, l2 + cnt, r2 - );
return root;
}
void dfs(int u, int sum) {
sum += u;
if (!lch[u] && !rch[u]) if (sum < mn || (sum == mn&&u < best)) {best = u; mn = sum;}
if (lch[u])dfs(lch[u], sum);
if (rch[u])dfs(rch[u], sum);
}
int main() {
while (read_list(in_order)) {
read_list(post_order);
build(, n - , , n - );
mn = 1e9;
dfs(post_order[n - ], );
cout << best << "\n";
} //system("pause");
}

【紫书】Tree UVA - 548 静态建树dfs的更多相关文章

  1. Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。

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

  2. UVA548——Tree(中后序建树+DFS)

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

  3. Tree UVA - 548

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

  4. 树(Tree,UVA 548)

    题目描述: 题目思路: 1.使用数组建树 //递归 2.理解后序遍历和中序遍历,建立左右子树 3.dfs深度搜索找出权重最小的路径 #include <iostream> #include ...

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

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

  6. UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

    Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...

  7. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

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

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

  9. 紫书 例题 11-13 UVa 10735(混合图的欧拉回路)(最大流)

    这道题写了两个多小时-- 首先讲一下怎么建模 我们的目的是让所有点的出度等于入度 那么我们可以把点分为两部分, 一部分出度大于入度, 一部分入度大于出度 那么显然, 按照书里的思路,将边方向后,就相当 ...

随机推荐

  1. 浅谈Java的学习

    Java就是用来做项目的!Java的主要应用领域就是企业级的项目开发!要想从事企业级的项目开发,你必须掌握如下要点: 1.掌握项目开发的基本步骤2.具备极强的面向对象的分析与设计技巧3.掌握用例驱动. ...

  2. SpringMVC -- 梗概--源码--贰--上传

    1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...

  3. CentOS下安装高版本GCC

    CentOS下安装高版本GCC 微信分享:   有时编译需要用到4.8以上版本的GCC,由于CentOS源没有提供高版本的GCC安装包,这时就不能通过安装包安装.通常的解决方案就是通过编译安装高版本的 ...

  4. window.open被拦截

    1)直接调用window.open 或 点击的时候直接调用 window.open 是不会被拦截的 // 不会被拦截$('.btn-open').click(function(){ window.op ...

  5. 【RF库XML测试】Get Element Text

    Name:Get Element TextSource:XML <test library>Arguments:[ source | xpath=. | normalize_whitesp ...

  6. Unity绘制Png图片

    using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; publ ...

  7. apache下配置php环境

    1. apache下载 http://httpd.apache.org/download.cgi 2. php下载 http://windows.php.net/download/ 3. 配置 apa ...

  8. iOS - 选取相册中iCloud云上图片和视频的处理

    关于iOS选取相册中iCloud云上图片和视频  推荐看:TZImagePickerController的源码,这个是一个非常靠谱的相册选择图片视频的库 .当然也可以自己写 如下遇到的问题 工作原因, ...

  9. DLL注入之Appinit_Dlls

    AppInit_DLLs is a mechanism that allows an arbitrary list of DLLs to be loaded into each user mode p ...

  10. 解析pdb文件得到未导出变量地址(转)

    程序要用到dbghelp.dll中的一些函数 http://msdn.microsoft.com/en-us/library/ms679291%28VS.85%29.aspx 要自己下载系统对应的符号 ...