Tree UVA - 548(二叉树递归遍历)
题目链接: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(二叉树递归遍历)的更多相关文章
- Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- 数据结构之二叉树篇卷二 -- 二叉树递归遍历(With Java)
一.先序递归遍历(Preorder Recursive Traversal) 1.1 算法 首先需要明确的是这里的序是针对 root 节点而言的.故先序即先“访问”根节点,其次“访问”其左右节点. 1 ...
- [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历
关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...
- UVa 548 (二叉树的递归遍历) Tree
题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍 ...
- Uva 548 二叉树的递归遍历lrj 白书p155
直接上代码... (另外也可以在递归的时候统计最优解,不过程序稍微复杂一点) #include <iostream> #include <string> #include &l ...
- java 二叉树递归遍历算法
//递归中序遍历 public void inorder() { System.out.print("binaryTree递归中序遍历:"); inorderTraverseRec ...
- python实现二叉树递归遍历与非递归遍历
一.中序遍历 前中后序三种遍历方法对于左右结点的遍历顺序都是一样的(先左后右),唯一不同的就是根节点的出现位置.对于中序遍历来说,根结点的遍历位置在中间. 所以中序遍历的顺序:左中右 1.1 递归实现 ...
- Tree UVA - 548
You are to determine the value of the leaf node in a given binary tree that is the terminal node o ...
- leetcode Binary Tree Postorder Traversal 二叉树后续遍历
先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...
随机推荐
- 5.移动渗透测试工具之drozer
本篇博文主要来介绍drozer在移动渗透中的作用 本次实验环境所用工具为:夜神模拟器,drozer-2.3.4,sieve.apk,adb drozer安装这里不再多嘴,给出链接:https://ww ...
- 输入框获取焦点后placeholder文字消失、修改placeholder的样式
输入框获取焦点后placeholder文字消失: <input placeholder='' onfocus="this.placeholder=''" onblur=&qu ...
- C/C++中char* p = "hello" 和 const char* p = "hello"的区别
在写代码常常都会写char * p ="hello";这样的代码,虽然不是错误,但却不建议这样用.应该加const修饰.这句话背后的内涵是什么?下面就刨根问底一下:) 这个行为在不 ...
- [SinGuLaRiTy] 2017-07-26 综合性测试
[SinGuLaRiTy-1032] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 单词 (word) 题目描述 ...
- Sum of LCM (lcmsum)
题目 [题目描述] 对于 $ A_1, A_2, \ldots, A_N $ ,求$\sum_{i = 1}^N \sum_{j = 1}^N \mathrm{lcm}(A_i, A_j)$ 的值. ...
- P4196 [CQOI2006]凸多边形 半平面交
\(\color{#0066ff}{题目描述}\) 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n=2时,两个凸多边形如下图: 则相交部分的面积为5.233. \(\color{#0066f ...
- USACO 1.1.1 YOUR RIDE IS HERE
众所周知,在每一个彗星后都有一只UFO.这些UFO时常来收集地球上的忠诚支持者.不幸的是,他们的飞碟每次出行都只能带上一组支持者.因此,他们要用一种聪明的方案让这些小组提前知道谁会被彗星带走.他们为每 ...
- windows下eclipse远程连接hadoop集群开发mapreduce
转载请注明出处,谢谢 2017-10-22 17:14:09 之前都是用python开发maprduce程序的,今天试了在windows下通过eclipse java开发,在开发前先搭建开发环境.在 ...
- Jenkins+Git+Maven+Nexus+Tomcat
https://www.jianshu.com/p/d24e64559440 https://blog.csdn.net/u013322876/article/details/72637854 htt ...
- html5 语音识别 转