紫书: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. Java:EL表达式

    ylbtech-Java:EL表达式 EL(Expression Language) 是为了使JSP写起来更加简单.表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 ...

  2. EasyUI Tree 树

    转自:http://www.jeasyui.net/plugins/185.html 通过 $.fn.tree.defaults 重写默认的 defaults. 树(tree)在网页中以树形结构显示分 ...

  3. Helios Service Release 2安装SVN

    Eclipse Helios Service Release 2安装SVN地址:subclipse http://subclipse.tigris.org/update_1.6.x 1.使用方式:he ...

  4. git 删除本地仓库

    更新: 2017/06/27 修改格式,备注mac下的命令没测试过   windows: rm .git/ mac: sudo rm -rf .git/ 没验证

  5. jwt的应用生成token,redis做储存

    解释一下JWT JWT就是一个字符串,经过加密处理与校验处理的字符串,由三个部分组成.基于token的身份验证可以替代传统的cookie+session身份验证方法.三个部分分别如下: header. ...

  6. 每天学点linux命令之nc

    nc is NetCat.素以短小精悍著称的网络工具包.主要用来开放的扫描端口(黑客或者OSAdmin的最爱),不同主机之间传输文字 | 文件. http://blog.csdn.net/zhangx ...

  7. [转]windows 7 下快速搭建php环境(windows7+IIS7+php+mysql)

    转贴:http://apps.hi.baidu.com/share/detail/10406992 (1).采用理由: 优点:最大化的桌面图形化操作系统,可维护性优秀.基于IIS v6.0/v7.0( ...

  8. 修改 进程占用资源限制ulimit(限制服务器的链接数目)

    ulimit用于限制shell启动进程所占用的资源.其中ulimit -n用于限制进程能够打开的文件描述符的最大数目.因为任何设备在linux下都是文件,通信的接口也有专门的接口文件负责,所以linu ...

  9. selenium学习第三天,新建一个测试用例(运行失败)。

    今天的意外收获,在找SELENIUM实例的时候,发现一个JS实例,功能各类非常全演示及代码都有,谢谢大神的分享:http://www.miniui.com/demo/#src=datagrid/pag ...

  10. HDU_1421_搬寝室_dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1421 搬寝室 Time Limit: 2000/1000 MS (Java/Others)    Me ...