Uva548 Tree
Tree
You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.
Input 
The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree. All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have more than 10000 nodes or less than 1 node.
Output 
For each tree description you should output the value of the leaf node of a path of least value. In the case of multiple paths of least value you should pick the one with the least value on the terminal node.
Sample Input 
3 2 1 4 5 7 6
3 1 2 5 6 7 4
7 8 11 3 5 16 12 18
8 3 11 7 16 18 12 5
255
255
Sample Output 
1
3
255
题意:给你一个二叉树的中序遍历和后序遍历,每一个点的序号就是这个点的权值,请求出从根节点到叶子节点权值和最小的那个节点,如果有多个,则输出叶子节点最小的.
分析:这道题很水,根据中序遍历和后序遍历可以很容易地建立一棵树,然后dfs一边就可以了。
至于怎么建树呢?后序遍历的最后一个点就是根节点,在中序遍历中找到这个位置,然后左边就是左子树的中序遍历,右边就是右子树的中序遍历,递归一下就能解决问题.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <sstream> using namespace std; const int maxn = ,inf = 0x7ffffff;
int in[maxn], post[maxn], tot,l[maxn],r[maxn],res,ans = inf; bool read1()
{
string s;
if (!getline(cin, s))
return false;
stringstream ss(s);
tot = ;
int x;
while (ss >> x)
in[tot++] = x;
return tot > ;
} void read2()
{
string s;
if (!getline(cin, s))
return;
stringstream ss(s);
tot = ;
int x;
while (ss >> x)
post[tot++] = x;
} int build(int l1, int r1, int l2, int r2)
{
if (l1 > r1)
return ;
int root = post[r2];
int o = l1;
while (in[o] != root)
o++;
int cnt = o - l1;
l[root] = build(l1, o - , l2, l2 + cnt - );
r[root] = build(o + , r1, l2 + cnt, r2 - );
return root;
} void dfs(int u, int sum)
{
if (!l[u] && !r[u])
{
if (sum < ans || (sum == ans && u < res))
{
res = u;
ans = sum;
}
}
if (l[u])
dfs(l[u], sum + l[u]);
if (r[u])
dfs(r[u], sum + r[u]);
} int main()
{
while (read1())
{
read2();
build(, tot - , , tot - );
ans = inf;
dfs(post[tot - ], post[tot - ]);
printf("%d\n", res);
} return ;
}
Uva548 Tree的更多相关文章
- 【日常学习】【二叉树遍历】Uva548 - Tree题解
		
这道题目本身不难,给出后序遍历和中序遍历,求到节点最小路径的叶子,同样长度就输出权值小的叶子. Uva上不去了,没法測.基本上是依照ruka的代码来的.直接上代码 //Uva548 Tree #inc ...
 - UVA548——Tree(中后序建树+DFS)
		
Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...
 - UVA548 Tree (二叉树的遍历)
		
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
 - UVA548 tree的思路
		
唔,首先这题给出了中序遍历和后序遍历要求我们求出, 一个叶子节点到根的数值总和最小,且这个叶子节点是最小的那个 这题的难点在于如何运用中序遍历和后序遍历还原整棵树, 这里有两个方法: 1. 递归构造原 ...
 - 例题6-8 Tree Uva548
		
这道题我一直尝试用scanf来进行输入,不过一直没有成功,因此先搁置一下,以后积累些知识再进行尝试. 这道题有两种解决方案: 即先建树,再遍历和边建树边遍历.这两种方案经过实践证实效率相差不太多.应该 ...
 - 二叉树的递归遍历 Tree UVa548
		
题意:给一棵点带权的二叉树的中序和后序遍历,找一个叶子使得他到根的路径上的权值的和最小,如果多解,那该叶子本身的权值应该最小 解题思路:1.用getline()输入整行字符,然后用stringstre ...
 - 作业2.7_3(给UVA548 树 Tree单独一个帖子)🍺
		
代码:(输入函数很香建议保留)我不理解他是绿的但 The Blocks Problem 是黄的 #include<bits/stdc++.h> using namespace std; i ...
 - Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。
		
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
 - [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
		
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
 
随机推荐
- $Hdu1381\ Crazy\ Search$
			
前置芝士 :string 的 基本用法 string s = "hello world" ; string tmp(s,0,5) ; cout << tmp <& ...
 - mui 文件上传注意问题
			
1. mui 文件上传 key对应后台接收参数名,但对对于多文件上传就没办法了,addFile 的key不能重复 task.addFile( "_www/a.doc", {key: ...
 - Linux学习之路2 Bash的基本操作
			
一.SHELL的介绍 shell分为两种:CLI(command Line Interface)和GUI(Graphical User Interface) 操作系统中的shell: GUI:GNOM ...
 - 301 Remove Invalid Parentheses 删除无效的括号
			
删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果.注意: 输入可能包含了除 ( 和 ) 以外的元素.示例 :"()())()" -> ["()()() ...
 - 17 C#中的循环执行 while循环
			
在编程中有代码的执行主要有三种方式.(1)顺序执行,也就是一条语句一条语句按顺序执行:(2)条件执行,也就是if...else.当某种条件满足时执行一些代码:(3)循环执行,就是当某种条件满足的时候, ...
 - Sql2008事务日志已满处理
			
处理方式: USE [master] GO ALTER DATABASE gzl SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE gzl SET ...
 - [ SDOI 2006 ] 保安站岗
			
\(\\\) Description 给出一棵 \(n\) 个节点以 \(1\) 为根的树,一个节点的覆盖半径是 \(1\) ,点有点权 \(val_x\) . 选择一些点,使得点权和最小,同时每个节 ...
 - Redis 注册为 widows 服务
			
redis-server.exe --service-install redis.windows.conf --loglevel verbose
 - java虚拟机(六)--垃圾收集器和内存分配策略
			
目前没有完美的收集器,不同的厂商.版本的虚拟机提供的垃圾收集器会有很大的差别,用户根据自己应用特点和要求组合出各个年代所使用 的收集器.基于jdk1.7Update14之后的虚拟机. HotSpot的 ...
 - MS SQL Server查询 本日、本周、本月、本季度、本年起始时间
			
参数声明 declare @beginTime datetime, --查询开始时间 @endTime datetime, --查询结束时间 @queryTimeType tinyint; --查询时 ...