Uva 548 Tree
0.这是一道利用中序遍历和后序遍历确定二叉树的题目,学会建树
关键点理解这段代码
int build(int L1,int R1,int L2,int R2)
{
//printf("built:\n");
;//空树
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;
}
1.剩下的就是递归了 注意一下递归边界是 到达叶子结点 即左右子树均为空的结点 就行了
if(!lch[v] && !rch[v])
#include <cstdio>
#include <iostream>
#include <cstring>
#include <sstream>
#include <algorithm>
using namespace std;
+ ;
int post_order[maxn],in_order[maxn],lch[maxn],rch[maxn];
int n,ans_sum,ans_v;
bool input(int*a)
{
string s;
//getline(cin,s);
if(!getline(cin,s)) return false;
stringstream ss(s);
n=;
int x;
while(ss>>x) a[n++]=x;
;
}
int build(int L1,int R1,int L2,int R2)
{
//printf("built:\n");
;//空树
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 v,int sum)
{
sum+=v;
if(!lch[v] && !rch[v])
{
if(sum < ans_sum)
ans_sum = sum,ans_v=v;
else if(sum == ans_sum && v < ans_v)
ans_v = v;
}
if(lch[v]) dfs(lch[v],sum);
if(rch[v]) dfs(rch[v],sum);
}
int main()
{
while(input(in_order))
{
input(post_order);
build(,n-,,n-);
ans_sum=;
dfs(post_order[n-],);
printf("%d\n",ans_v);
}
;
}
Uva 548 Tree的更多相关文章
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- 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 (建树+前序后序)
Description You are to determine the value of the leaf node in a given binary tree that is the termi ...
- UVa 548 Tree【二叉树的递归遍历】
题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...
- UVA 548 Tree 建树
题意: 输入中序和后序的权值,输出哪个叶子使它到根的路径上权和最小. 思路: 输入后建树,然后dfs求最小的叶子. #include<iostream> #include<cstdi ...
- uva 548 Tree(通过后序,先序重建树+dfs)
难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...
- UVA - 548 Tree(二叉树的递归遍历)
题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #inc ...
- UVa 548 Tree(中序遍历+后序遍历)
给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...
- Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
随机推荐
- MPAndroidChart 教程
以前没用过MPAndroidChart,为了方便学习查找,引用下别个大神的笔记. 其余文章索引: MPAndroidChart 教程:概述MPAndroidChart 教程:开始 Getting St ...
- NEFU 84 五指山 (扩展欧几里得)
五指山 Problem:84 Time Limit:1000ms Memory Limit:65536K Description 西游记中孙吾空大闹天宫,如来佛祖前来降伏他,说道:"我与你打 ...
- centos7 php7 httpd
安装php之前,要先安装几个 1.下载php源码:http://cn2.php.net/distributions/php-7.0.6.tar.gz. 2.然后使用命令:tar -zxvf php-7 ...
- python基础——第三方模块
python基础——第三方模块 在Python中,安装第三方模块,是通过包管理工具pip完成的. 如果你正在使用Mac或Linux,安装pip本身这个步骤就可以跳过了. 如果你正在使用Window ...
- 点击按钮对两个div的隐藏与显示进行切换
HTML: <button type="button" id="showHidden">点击切换div的隐藏与显示</button> ...
- C++面向对象基础知识
多态是为了接口重用,封装和继承是为了代码重用 子类重新定义父类虚函数的方法叫做继承,不是重载! 一.基本概念 对于C++中经常出现的函数名称相同但是参数列表或者返回值不同的函数,主要存在三种情况: 1 ...
- Ionic2 Tutorial
build your first app Now that you have Ionic and its dependencies installed, you can build your firs ...
- case/casez/casex 的区分与使用
参考:http://www.cnblogs.com/poiu-elab/archive/2012/11/02/2751323.html 与 verilog数字系统设计基础 一般来说,使用最多的是CA ...
- sql server 的cpu使用率过高的分析
有哪些SQL语句会导致CPU过高? 1.编译和重编译 编译是 Sql Server 为指令生成执行计划的过程.Sql Server 要分析指令要做的事情,分析它所要访问的表格结构,也就是生成执行计划的 ...
- 解决postgresql -- ERROR: 42601: query has no destination for result data
I am learning Npgsql and PostgreSQL. I am unable to define the output parameter correctly. What am I ...