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 ...
随机推荐
- 如何让两个 并列的div高度相等
哪个div Height值大,就将其值赋给Height值小的div,从而使2个div高度始终保持一致. function $(id){ return document.getElementById(i ...
- 【leetcode】Number of 1 Bits (easy)
做太多遍了,秒杀. class Solution { public: int hammingWeight(uint32_t n) { ; ), num++); return num; } };
- 【编程题目】如何对n个数进行排序,要求时间复杂度O(n),空间复杂度O(1)
转自:http://blog.csdn.net/vast_sea/article/details/8167968 看上去似乎任何已知的算法都无法做到,如果谁做到了,那么所有的排序方法:QuickSor ...
- 解决Eclipse里Maven工程报 An error occurred while filtering resources错误
这几天被maven的单元测试折腾死了,以为是自己的eclipse有问题呢,今天早上来了又发现eclipse报了一个很奇怪的错误:An error occurred while filtering re ...
- 一分钟可知css3版大白源码
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- iOS - 直播相关文章
直播相关文章 直播RTMP可用于测试的服务器地址 FFmpeg avdumpformat输出的tbn.tbc.tbr.PAR.DAR的含义 FFmpeg 3.0 计算视频时长 HLS Streamin ...
- PHP中的常用魔术方法
魔术方法: 是指某些情况下,会自动调用的方法,称为魔术方法 php面向对象中,提供了这几个魔术方法,他们的特点都是 以双下划线__开头的 __construct() 构造方法 __destruct( ...
- windows重新获取IP
win+r------->cmd------>ipconfig /release (释放ip) ipconfig /renew 重新获取ip
- BlueTooth: 蓝牙基础知识进阶——链路控制操作
转自:http://blog.csdn.net/augusdi/article/details/25887395 七链路控制操作 链路控制操作就是用来描述一个设备是如何加入piconet又是如何从一个 ...
- **PHP中替换换行符
PHP中替换换行符 php 不同系统的换行不同系统之间换行的实现是不一样的linux 与unix中用 \nMAC 用 \rwindow 为了体现与linux不同 则是 \r\n所以在不同平台上 实现方 ...