题目链接:https://vjudge.net/problem/UVA-548

题目大意:给一颗点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序遍历和后序遍历,找一个叶子结点使得它到根的路径上的权值和最小。如果有多个解,该叶子本身的权值应尽量小。输入中每两行表示一棵树,其中第一行为中序遍历,第二行为后续遍历。

看代码:

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<queue>
#include<sstream>
using namespace std;
const int maxv=+;
int in_order[maxv],post_order[maxv],lch[maxv],rch[maxv];
int n;
int best,best_sum;//目前为止最优解和对应的权值和
bool read_list(int *a)
{ string line;
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)
{
if(L1>R1) return ;//空树
int root=post_order[R2];//后序遍历最后一个结点就是树根
int p=L1;
while(in_order[p]!=root) p++;//在中序遍历中找到根结点所在的位置
int cnt1=p-L1;//左子树结点的个数
lch[root]=build(L1,p-,L2,L2+cnt1-);
rch[root]=build(p+,R1,L2+cnt1,R2-);
return root;
}
void dfs(int u,int sum)
{
sum+=u;
if(!lch[u]&&!rch[u])//代表叶子结点
{
if(sum<best_sum||(sum==best_sum&&u<best))
{
best=u;
best_sum=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-);
best_sum=;
dfs(post_order[n-],);
cout<<best<<endl;
}
return ;
}

Tree UVA - 548(二叉树递归遍历)的更多相关文章

  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. 数据结构之二叉树篇卷二 -- 二叉树递归遍历(With Java)

    一.先序递归遍历(Preorder Recursive Traversal) 1.1 算法 首先需要明确的是这里的序是针对 root 节点而言的.故先序即先“访问”根节点,其次“访问”其左右节点. 1 ...

  3. [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历

    关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...

  4. UVa 548 (二叉树的递归遍历) Tree

    题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍 ...

  5. Uva 548 二叉树的递归遍历lrj 白书p155

    直接上代码... (另外也可以在递归的时候统计最优解,不过程序稍微复杂一点) #include <iostream> #include <string> #include &l ...

  6. java 二叉树递归遍历算法

    //递归中序遍历 public void inorder() { System.out.print("binaryTree递归中序遍历:"); inorderTraverseRec ...

  7. python实现二叉树递归遍历与非递归遍历

    一.中序遍历 前中后序三种遍历方法对于左右结点的遍历顺序都是一样的(先左后右),唯一不同的就是根节点的出现位置.对于中序遍历来说,根结点的遍历位置在中间. 所以中序遍历的顺序:左中右 1.1 递归实现 ...

  8. Tree UVA - 548

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

  9. leetcode Binary Tree Postorder Traversal 二叉树后续遍历

    先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...

随机推荐

  1. Entity Framework Tutorial Basics(17):DBSet Class

    DBSet Class DBSet class represents an entity set that is used for create, read, update, and delete o ...

  2. SQL Server相关知识和经验的碎片化记录

    1.在向服务器发送请求时发生传输级错误 在向服务器发送请求时发生传输级错误. (provider: TCP 提供程序, error: 0 - 远程主机强迫关闭了一个现有的连接.) ---> Sy ...

  3. Bugly升级应用集成指南

    1.配置 app/build.gradle android { defaultConfig { ndk { //设置支持的SO库架构 abiFilters 'armeabi' //, 'x86', ' ...

  4. AppDelegate生命周期回调顺序

    1. 应用初次启动: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDiction ...

  5. 数组谓词查询法 NSPredicate

    NSPredicate:谓词 字面翻译是这个意思,但是我觉得谓词这个词太难以理解了 NSPredicate的具体用途应该还是过滤,类似于过滤条件之类的,相当于一个主语的谓语,所以说会是谓词这个名字.( ...

  6. [hdu 2089] 不要62 数位dp|dfs 入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求[n, m]区间内不含4和62的数字个数. 这题有两种思路,直接数位dp和dfs 数位d ...

  7. tcp设置超时重传

    TCP超时和重传的基础是怎样根据给定连接RTT设置RTO,若TCP先于RTT开始重传,可能会在网络中引入不必要的重复数据,反之,若延迟至远大于RTT的间隔发送重传数据,整体网络利用率会随之下降.由于R ...

  8. CF1005F Berland and the Shortest Paths

    \(\color{#0066ff}{ 题目描述 }\) 一个无向图(边权为1),输出一下选边的方案使\(\sum d_i\)最小(\(d_i\)为从1到i的最短路) 输出一个方案数和方案(方案数超过k ...

  9. SP263 PERIOD - Period KMP技巧

    \(\color{#0066ff}{题目描述}\) 如果一个字符串S是由一个字符串T重复K次形成的,则称T是S的循环元.使K最大的字符串T称为S的最小循环元,此时的K称为最大循环次数. 现给一个给定长 ...

  10. VUE使用微信JDK(附踩坑记录)

    VUE使用微信分享SDK(附踩坑记录) 微信分享官方文档 安装JS-SDK npm i -S weixin-jsapi 引入包 ES5 写法 const wx = require('weixin-js ...