紫书: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. C#直接删除指定目录下的所有文件及文件夹(保留目录)

    #region 直接删除指定目录下的所有文件及文件夹(保留目录) /// <summary> /// 直接删除指定目录下的所有文件及文件夹(保留目录) /// </summary&g ...

  2. 3-1 vue生存指南 - todolist实现-数据渲染

    由于Vue.js作者是中国人,会说汉语,所以国内生态会更好一点.Vue.js作者是尤雨溪,

  3. BACnet开发资料与调试工具

    一.开发资料 1.认识BACnet协议 2.BACnet网络讲义: 链接:https://pan.baidu.com/s/1A6OOUxvJe1zIYbockqTEsQ提取码:wz49 二.调试工具 ...

  4. 【BZOJ4059】Non-boring sequences(分析时间复杂度)

    题目: BZOJ4059 分析: 想了半天没什么想法,百度到一个神仙做法-- 设原数列为 \(a\),对于每一个 \(i\) 求出前一个和后一个和 \(a_i\) 相等的位置 \(pre[i]\) 和 ...

  5. java 配置信息类 Properties 的简单使用

    Properties :(配置信息类) 是一个表示持久性的集合 ,继承 Hashtable ,存值是以键-值得方式  主要用于生产配置文件和读取配置文件信息. 简单的实例: import java.i ...

  6. myeclipse中部署svn

    一.下载SVN插件subclipse 下载地址:http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240 在打开的网 ...

  7. poj3279 Fliptile

    思路: 枚举. 枚举了第一行的操作之后,下面每行的操作也随之确定了.因为在确定了第i行的操作之后,要想再改变a[i][j]的状态只能通过改变a[i + 1][j]来实现.另外,用到了集合的整数表示方法 ...

  8. Android基础TOP7_1:ListView制作列表

    结构: Activity: activity_main: <RelativeLayout xmlns:android="http://schemas.android.com/apk/r ...

  9. RPC——笔记

    整理的笔记来源:https://mp.weixin.qq.com/s/JkXrPcuKtE2qYgmDcH2uww RPC(远程过程调用): RPC是:一个计算机通信协议. 调用过程:计算机 A 上的 ...

  10. Greenplum开发

    Greenplum(GP)采用了MPP架构,基于开源的数据库 PostgreSQL(PG). 1.首先什么是MPP架构? GreenPlum的架构采用了MPP(大规模并行处理).在 MPP 系统中,每 ...