紫书:P155

uva  548

 

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

给出一棵树的后序和中序遍历结果,要求求出从某个叶子节点回到树根的最小叶子节点,注意这是一颗带权树

后序遍历的最后一个节点是树根,而中序遍历的树根在中间,而且其左边全都是左子树子孙,右边是右子树子孙。

那么可以这样做:

1.从后序遍历中取出根

2.在中序遍历中找到根的位置,并以此位置把序列分为左子树和右子树

3.递归地对左子树和右子树执行1,2操作

#include <iostream>
#include <sstream>
using namespace std;
const int maxsize=1e4+;
int In_order[maxsize],Post_order[maxsize],lchild[maxsize],rchild[maxsize];
int Shortest_path;
int Shortest_path_node;
int n; bool input(int *a)
{
string line;
if(!getline(cin,line)) return false;
n=;
stringstream ss(line);
while(ss>>a[n]) n++;
return n>;
} 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;
lchild[root]=Build(L1,p-,L2,L2+cnt-);
rchild[root]=Build(p+,R1,L2+cnt,R2-);
return root;
} void dfs(int u,int sum)
{
sum+=u;
if(!lchild[u]&&!rchild[u])
{
if(sum<Shortest_path||(sum==Shortest_path&&u<Shortest_path_node))
{
Shortest_path=sum;
Shortest_path_node=u;
}
}
if(lchild[u]) dfs(lchild[u],sum);
if(rchild[u]) dfs(rchild[u],sum);
} int main()
{
while(input(In_order))
{
input(Post_order);
Build(,n-,,n-);
Shortest_path=1e9;
dfs(Post_order[n-],);
cout<<Shortest_path_node<<endl;
}
return ;
}

Tree(树的还原以及树的dfs遍历)的更多相关文章

  1. Vasya and a Tree CodeForces - 1076E(线段树+dfs)

    I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...

  2. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  3. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  4. 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )

    在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...

  5. 【CF725G】Messages on a Tree 树链剖分+线段树

    [CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...

  6. dfs序+主席树 或者 树链剖分+主席树(没写) 或者 线段树套线段树 或者 线段树套splay 或者 线段树套树状数组 bzoj 4448

    4448: [Scoi2015]情报传递 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 588  Solved: 308[Submit][Status ...

  7. ACM学习历程——POJ3321 Apple Tree(搜索,线段树)

          Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will ...

  8. [学习笔记]dsu on a tree(如何远离线段树合并)

    https://www.zybuluo.com/ysner/note/1318613 背景 这玩意来源于一种有局限性的算法. 有一种广为人知的,树上离线维护子树信息的做法. (可以参照luogu360 ...

  9. TTTTTTTTTTTT Gym 100818B Tree of Almost Clean Money 树连剖分+BIT 模板题

    Problem B Tree of Almost Clean Money Input File: B.in Output File: standard output Time Limit: 4 sec ...

随机推荐

  1. Eclipse 配置 Python 环境

    1.将下载好的Pydev4.5.2(http://sourceforge.net/projects/pydev/files/pydev/  里面有很多版本) 文件夹里的两个文件夹(features+p ...

  2. 昆石VOS3000_2.1.2.0完整安装包及安装脚本

    安装包下载地址 http://www.51voip.org/post/57.html 安装教程: 上传安装包 ·给整个目录授权 chmod 777 /root/vosintsall 1.安装前准备 首 ...

  3. fck 属性配置大全

    优化FCKeditor文件夹和文件: 下载FCKeditor并解压之后,会产生_samples和 editor两个文件夹和几个文件,全部删除以_开头的文件夹和文件,因为这些都是FCKeditor的一些 ...

  4. 【转载】Cookie/Session机制详解

    [本文转自]http://blog.csdn.net/fangaoxin/article/details/6952954/ 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话. ...

  5. bzoj21012101: [Usaco2010 Dec]Treasure Chest 藏宝箱(滚动数组优化dp)

    2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 592  Solved:  ...

  6. poj2096Collecting Bugs(概率期望dp)

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 6400   Accepted: 3128 ...

  7. bzoj1051受欢迎的牛(Tarjan)

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4776  Solved: 2542 Description ...

  8. Hadoop伪分布式模式搭建

    title: Hadoop伪分布式模式搭建 Quitters never win and winners never quit. 运行环境: Ubuntu18.10-server版镜像:ubuntu- ...

  9. 由一维数组表示的N维数组实现(C++)

    工作中,经常需要表示多维数组(如二维矩阵),常见的做法是使用T **pArr: T **pArr = new T*[M];//创建二维数组[M][N] ;i<M;i++) { pArr[i] = ...

  10. [ GDOI 2014 ] 拯救莫莉斯

    \(\\\) \(Description\) 有一个 \(N\times M\) 的网格,每个格点都有权值,图是四连通的. 现在选择一个点集,使得每个格点要么被选中,要么连通的点之一被选中. 求这个点 ...