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)便是使用二叉树实现 ...
随机推荐
- 清北考前刷题day4下午好
/* 辗转相除,每次计算多出现了几个数. */ #include<iostream> #include<cstdio> #include<cstring> #inc ...
- Poj 3694 Network (连通图缩点+LCA+并查集)
题目链接: Poj 3694 Network 题目描述: 给出一个无向连通图,加入一系列边指定的后,问还剩下多少个桥? 解题思路: 先求出图的双连通分支,然后缩点重新建图,加入一个指定的边后,求出这条 ...
- ZOJ-3960 What Kind of Friends Are You?
What Kind of Friends Are You? Time Limit: 1 Second Memory Limit: 65536 KB Japari Park is a larg ...
- Linux上安装wine qq的方法
linxu上安装QQ的发 百度网盘 提取码:f2sn 步骤一.安装wine(详见:https://www.winehq.org/download) // ubuntu/ubuntukylin/mint ...
- 51nod1459 迷宫游戏
1459 迷宫游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你 ...
- 记录sql操作
需求:一个a表的A列等于b表的B列 但拥有的相同列C列值不相同 需要将其改成一样的 UPDATE vd_auth_switch vas,tb_student ts set vas.class_id = ...
- TensorFlow---基础---GFile
使用TensorFlow的时候经常遇到 tf.gfile.exists().... 关于gfile,一个googler是这样给出的解释: The main roles of the tf.gfile ...
- win10下spark+Python开发环境配置
Step0:安装好Java ,jdk Step1:下载好: Step2: 将解压后的hadoop和spark设置好环境变量: 在系统path变量里面+: Step3: 使用pip安装 py4j : p ...
- c++自动导出lua绑定
cocos 使用bindings-generator脚本代替了toLua++. 编写效率大大提高. 具体的在本机中分享:http://note.youdao.com/noteshare?id=0f41 ...
- droid开发:如何打开一个.dcm文件作为位图?
我目前正在做一个Android应用程序的DICOM 继code打开图片DROM RES /绘制的“ussual”图像格式,但它不与.dcm工作 公共类BitmapView扩展视图 { 公共Bitmap ...