Description

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
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
const int INF=0x5fffffff;
const double EXP=1e-;
const int mod=;
const int MS=; int in_order[MS],post_order[MS];
int lch[MS],rch[MS];
int n,best,best_sum; bool input()
{
string str;
if(!getline(cin,str))
return false;
stringstream ss(str);
int cnt=;
int x;
while(ss>>x)
in_order[cnt++]=x;
getline(cin,str);
stringstream s(str);
n=cnt;
cnt=;
while(s>>x)
post_order[cnt++]=x;
best_sum=INF;
return true;
} int build(int L1,int R1,int L2,int R2)
{ //返回树根
if(L1>R1)
return ;
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 root,int sum)
{
sum+=root;
if(!lch[root]&&!rch[root])
{
if(sum<best_sum||(sum==best_sum&&root<best))
{
best=root;
best_sum=sum;
}
}
if(lch[root])
dfs(lch[root],sum);
if(rch[root])
dfs(rch[root],sum);
} int main()
{
while(input())
{
build(,n-,,n-);
dfs(post_order[n-],);
cout<<best<<endl;
}
return ;
}

F - Tree的更多相关文章

  1. [atcoder contest 010] F - Tree Game

    [atcoder contest 010] F - Tree Game Time limit : 2sec / Memory limit : 256MB Score : 1600 points Pro ...

  2. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】

    传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...

  3. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) F. Tree Factory 构造题

    F. Tree Factory Bytelandian Tree Factory produces trees for all kinds of industrial applications. Yo ...

  4. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  5. AtCoder Grand Contest 010 F - Tree Game

    题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_f 题目大意: 给定一棵树,每个节点上有\(a_i\)个石子,某个节点上有一个棋子,两人轮流操 ...

  6. Day8 - F - Tree POJ - 1741

    Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v) ...

  7. Codeforces Round #527 (Div. 3) . F Tree with Maximum Cost

    题目链接 题意:给你一棵树,让你找一个顶点iii,使得这个点的∑dis(i,j)∗a[j]\sum dis(i,j)*a[j]∑dis(i,j)∗a[j]最大.dis(i,j)dis(i,j)dis( ...

  8. CF F - Tree with Maximum Cost (树形DP)给出你一颗带点权的树,dist(i, j)的值为节点i到j的距离乘上节点j的权值,让你任意找一个节点v,使得dist(v, i) (1 < i < n)的和最大。输出最大的值。

    题目意思: 给出你一颗带点权的树,dist(i, j)的值为节点i到j的距离乘上节点j的权值,让你任意找一个节点v,使得dist(v, i) (1 < i < n)的和最大.输出最大的值. ...

  9. Codeforces Round #527 F - Tree with Maximum Cost /// 树形DP

    题目大意: 给定一棵树 每个点都有点权 每条边的长度都为1 树上一点到另一点的距离为最短路经过的边的长度总和 树上一点到另一点的花费为距离乘另一点的点权 选定一点出发 使得其他点到该点的花费总和是最大 ...

随机推荐

  1. Remove Duplicates from Sorted List @LeetCode

    /** * Remove Duplicates from Sorted List * * Given a sorted linked list, delete all duplicates such ...

  2. 在WindowsServer2008服务器上安装SQLServer2008R2

    登录服务器 使用远程桌面登录Windows Server 2008   安装前的准备工作 下载SQL Server安装程序 下载Microsoft SQL Server2008 R2 RTM - Ex ...

  3. Vmware 虚拟的Linux系统如何与宿主主机共享上网

    学校局域网内的机器是经过一个计费登陆客户端Gmon上网的,我前两天刚用Vmware虚拟了一个Linux      Guest OS 用作测试用,在Vmware的VM>>Settings 里 ...

  4. XML与DataSet相互转换,DataSet查询

    以FileShare.Read形式读XML文件: string hotspotXmlStr = string.Empty; try { Stream fileStream = new FileStre ...

  5. 当类库项目中无法使用Application.StartupPath

    通常我们WinForm编程时,要获取程序当前运行的文件夹路径会用Application.StartupPath ,但是Application.StartupPath在编写类库项目时却无法使用,因为我们 ...

  6. 利用C#实现对excel的写操作

    一.COM interop 首先我们要了解下何为COM Interop,它是一种服务,可以使.NET Framework对象能够与COM对象通信.Visual Studio .NET 通过引入面向公共 ...

  7. 常见MFC UI界面库[转]

    Xtrme toolkit,BCGControlBar,SkinMagic,AppFace,Skin++,Uskin++,SYGUI,LibUIDK,GuiToolkit,GardenUI等等,除了后 ...

  8. 2014年QS世界大学排名

    新浪教育[微博]讯 近日2014QS世界大学排行榜发布,榜单前十强均为英美名校.其中麻省理工大学以绝对优势位居榜首:英国剑桥大学及帝国理工学院并列排名第二:哈佛大学较去年而言名次略微下降,跌至第四. ...

  9. linux 下使rdate命令支持ipv6 ntp server 同步时间

    如果使用linux 下,busybox自带的rdate命令 去ipv6 的ntp server 同步时间的话,会提示invalid argument :无效参数. 那么现在下载rdate的源码并对其进 ...

  10. 用NDK编译lua库

    Android.mk是这样的 LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := lua LOCAL_SRC_FILE ...