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的更多相关文章

  1. 【日常学习】【二叉树遍历】Uva548 - Tree题解

    这道题目本身不难,给出后序遍历和中序遍历,求到节点最小路径的叶子,同样长度就输出权值小的叶子. Uva上不去了,没法測.基本上是依照ruka的代码来的.直接上代码 //Uva548 Tree #inc ...

  2. UVA548——Tree(中后序建树+DFS)

    Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...

  3. UVA548 Tree (二叉树的遍历)

    You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...

  4. UVA548 tree的思路

    唔,首先这题给出了中序遍历和后序遍历要求我们求出, 一个叶子节点到根的数值总和最小,且这个叶子节点是最小的那个 这题的难点在于如何运用中序遍历和后序遍历还原整棵树, 这里有两个方法: 1. 递归构造原 ...

  5. 例题6-8 Tree Uva548

    这道题我一直尝试用scanf来进行输入,不过一直没有成功,因此先搁置一下,以后积累些知识再进行尝试. 这道题有两种解决方案: 即先建树,再遍历和边建树边遍历.这两种方案经过实践证实效率相差不太多.应该 ...

  6. 二叉树的递归遍历 Tree UVa548

    题意:给一棵点带权的二叉树的中序和后序遍历,找一个叶子使得他到根的路径上的权值的和最小,如果多解,那该叶子本身的权值应该最小 解题思路:1.用getline()输入整行字符,然后用stringstre ...

  7. 作业2.7_3(给UVA548 树 Tree单独一个帖子)🍺

    代码:(输入函数很香建议保留)我不理解他是绿的但 The Blocks Problem 是黄的 #include<bits/stdc++.h> using namespace std; i ...

  8. Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。

    You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...

  9. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

随机推荐

  1. MySQL索引使用以及优化

    优化后台业主评价服务人员运行缓慢.   案发现场:后台业主评价服务人员列表页以及搜索页运行缓慢.运行时间为24074ms.   排查过程: 1.代码开头加时间,结束加时间.看运行了多少秒. 2.给评价 ...

  2. MySQL replace into 用法(insert into 的增强版)

    转 http://blog.csdn.net/risingsun001/article/details/38977797 MySQL replace into 用法(insert into 的增强版) ...

  3. LN : leetcode 690 Employee Importance

    lc 690 Employee Importance 690 Employee Importance You are given a data structure of employee inform ...

  4. .Net实战之反射外卖计费

    场景 叫外卖支付,可以有以下优惠: 1.  满30元减12 2.  是会员减配送费,比如5元 3.  优惠券 …. 问题? 如何在不改代码的情况下更灵活的去控制优惠的变化??? 有些代码与实际业务可能 ...

  5. Code Kata:大整数四则运算—乘法 javascript实现

    上周练习了加减法,今天练习大整数的乘法运算. 采取的方式同样为竖式计算,每一位相乘后相加. 乘法函数: 异符号相乘时结果为负数,0乘任何数都为0 需要调用加法函数 因为输入输出的为字符串,需要去除字符 ...

  6. Android RxJava1.X升级到RxJava2.X笔记

    简书地址 http://www.jianshu.com/p/2badfbb3a33b 描述 RxJava 1.X RxJava 2.X package包名 rx.xxx io.reactivex.xx ...

  7. 联想 Z5S(L78071)免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖 ZUI 10.5.370

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...

  8. PHP开发心得三

    1, JSON在调用json_decode前要检查其中是否含有反斜杠“\”等特殊字符 比如下面这段代码,返回的就是空值,非常坑爹吧. $res = {"Ret":"1&q ...

  9. Windows IIS 集成PHP时修改PHP.ini 配置后不生效问题

    iis下修改c://windows/php.ini 重启iis的网站不生效.需要重启应用程序池即可生效.

  10. sql server nullif的使用技巧,除数为零的处理技巧

    在sql server中做除法处理的时候,我们经常需要处理除数为零的情况,因为如果遇到这种情况的时候,sqlserver会抛出遇到以零作除数错误的异常,我们总不希望把这个异常显示给用户吧. 做个会报这 ...