UVa 548 Tree【二叉树的递归遍历】
题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小。
学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点
虽然紫书的思路很清晰= =可是理解起来好困难啊啊啊啊
后来终于问懂一丢丢了---
比如说样例:
中序遍历:3 2 1 4 5 7 6
后序遍历:3 1 2 5 6 7 4
首先做第一层: 在后序遍历中的最后一个数为根节点,然后在到中序遍历中找到这个根节点,在这个根节点的左边是左子树,右边是右子树,这样就确定出了左子树和右子树的区间
然后做第二层 中序遍历左子树的长度等于后序遍历中左子树的长度 即为在这个样例中:
中序遍历的左子树为 3 2 1 后序遍历的左子树为3 1 2 然后又可以确定2为这颗左子树的一个根节点,又可以将 3 2 1划分成左右子树区间
就这样一层一层划分建树
概括一下就是:
中序遍历序列:【左子树区间中序遍历序列】【根】【右子树区间中序遍历序列】
后序遍历序列:【左子树区间中序遍历序列】【右子树区间中序遍历序列】【根】
话说build函数也理解了好久的说 = =
对于lch[root]=build(L1,p-1,L2,L2+cnt-1);L1到p-1是在中序遍历中的左子树 L2到L2+cnt-1是在后序遍历中的左子树
对于rch[root]=build(p+1,R1,L2+cnt,R2-1);p+1到R1是在中序遍历中的右子树
L2+cnt到R2-1是在后序遍历中的右子树
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<algorithm>
#include<sstream>
using namespace std; typedef long long LL;
const int maxn=+;
int in_order[maxn],post_order[maxn],lch[maxn],rch[maxn];
int n; 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 cnt=p-L1;//左子树的结点个数
lch[root]=build(L1,p-,L2,L2+cnt-);
rch[root]=build(p+,R1,L2+cnt,R2-);
return root;
} int best,best_sum; 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);//如果u有左孩子,继续深搜,直到搜到叶子结点
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<<"\n";
}
return ;
}
go---go---
UVa 548 Tree【二叉树的递归遍历】的更多相关文章
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- UVA 548(二叉树重建与遍历)
J - Tree Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Ap ...
- UVa 548 Tree(中序遍历+后序遍历)
给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...
- UVa 548 Tree(二叉树最短路径)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- UVa 548 (二叉树的递归遍历) Tree
题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍 ...
- Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- 二叉树的递归遍历 The Falling Leaves UVa 699
题意:对于每一棵树,每一个结点都有它的水平位置,左子结点在根节点的水平位置-1,右子节点在根节点的位置+1,从左至右输出每个水平位置的节点之和 解题思路:由于上题所示的遍历方式如同二叉树的前序遍历,与 ...
- 二叉树的递归遍历 Tree UVa548
题意:给一棵点带权的二叉树的中序和后序遍历,找一个叶子使得他到根的路径上的权值的和最小,如果多解,那该叶子本身的权值应该最小 解题思路:1.用getline()输入整行字符,然后用stringstre ...
- 数据结构之二叉树篇卷三 -- 二叉树非递归遍历(With Java)
Nonrecursive Traversal of Binary Tree First I wanna talk about why we should <code>Stack</c ...
- C++编程练习(17)----“二叉树非递归遍历的实现“
二叉树的非递归遍历 最近看书上说道要掌握二叉树遍历的6种编写方式,之前只用递归方式编写过,这次就用非递归方式编写试一试. C++编程练习(8)----“二叉树的建立以及二叉树的三种遍历方式“(前序遍历 ...
随机推荐
- apache + tomcat 集群
apache2.2与tomcat集成(可以多个tomcat) 需求概况: 有3个服务: localhost:9091, localhost:9190. localhost:9191分别对应3个tomc ...
- 文本编辑器 markdown
http://www.cnblogs.com/youxia/p/linux014.html markdown对数学公式的支持http://www.linuxidc.com/Linux/2014-08/ ...
- SVN提交错误:working copy is not up-to-date解决方法
我在项目中删了2个jar,然后SVN提交,一直提交不成功 svn在提交时报错如下图: working copy is not up-to-date svn:commit failed(details ...
- 把工程部署在tomcat的root路径下
myeclipse可以右键工程:(eclipse也可以)选择properties->myeclipse->web:把web context-root改成:/然后在用myeclipse部署项 ...
- ZedGrap控件绘制图表曲线
问题描述: 使用C#中ZedGrap控件绘制图表曲线图 ZedGrap 介绍说明: 安装ZedGrap控件 ZedGraph控件dll文件: 添加ZedGraph控件,首先在新建立的C#图像工 ...
- JQuery,UIbootstrap风格弹出层
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <met ...
- 2013 Asia Regional Changchun
Hard Code http://acm.hdu.edu.cn/showproblem.php?pid=4813 #include<cstdio> ]; int main(){ int t ...
- hdu 4901
一个简单的dp,比赛的时候太坚信自己的小聪明没用二维数组一直WA到死: #include<cstdio> #include<cstring> #define maxn 1009 ...
- lintcode :Segmemt Tree Build II
题目 Segmemt Tree Build II The structure of Segment Tree is a binary tree which each node has two attr ...
- ubuntu 修改主机及主机名
修改主机: sudo vim /etc/hostname sudo vim /etc/hosts 修改用户名: sudo vim /etc/passwd sudo mv /home/yinggc /h ...