Tree(树的还原以及树的dfs遍历)
紫书: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遍历)的更多相关文章
- Vasya and a Tree CodeForces - 1076E(线段树+dfs)
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...
- 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 ...
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- 主席树[可持久化线段树](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 ...
- 【CF725G】Messages on a Tree 树链剖分+线段树
[CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...
- dfs序+主席树 或者 树链剖分+主席树(没写) 或者 线段树套线段树 或者 线段树套splay 或者 线段树套树状数组 bzoj 4448
4448: [Scoi2015]情报传递 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 588 Solved: 308[Submit][Status ...
- ACM学习历程——POJ3321 Apple Tree(搜索,线段树)
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will ...
- [学习笔记]dsu on a tree(如何远离线段树合并)
https://www.zybuluo.com/ysner/note/1318613 背景 这玩意来源于一种有局限性的算法. 有一种广为人知的,树上离线维护子树信息的做法. (可以参照luogu360 ...
- 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 ...
随机推荐
- 3-1 todolist功能开发
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js实现页面的全屏与退出
1.在data里面定义: data(){ return{ isScreen:false, } } //是否显示全屏 fullScreen(event){ this.isScreen = !this.i ...
- bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形【叉积+极角排序+瞎搞】
参考:https://blog.csdn.net/u012288458/article/details/50830498 有点神啊 正难则反,考虑计算不符合要求的三角形.具体方法是枚举每个点,把这个点 ...
- SP2916 GSS5 - Can you answer these queries V
给定一个序列.查询左端点在$[x_1, y_1]$之间,且右端点在$[x_2, y_2]$之间的最大子段和,数据保证$x_1\leq x_2,y_1\leq y_2$,但是不保证端点所在的区间不重合 ...
- Y-C
1.asp.net服务控件生命周期 11个生命阶段 (1)初始化: 初始化在传入Web请求生命周期内所需的设置,.跟踪视图状态.页面框架通过默认方式引发Init事件,并调用OnInit()方法,控件开 ...
- c语言—栈区,堆区,全局区,文字常量区,程序代码区 详解
转:http://www.cnblogs.com/xiaowenhui/p/4669684.html 一.预备知识—程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分1.栈区(sta ...
- linux学习之路6 Vi文本编辑器
vim是vi的增强版本 vim拥有三种模式: 命令模式(常规模式) vim启动后,默认进入命令模式.任何模式都可以通过按esc键回到命令模式(可以多按几次.命令模式可以通过键入不同的命令完成选择.复制 ...
- Android 性能优化(5)网络优化 (1) Collecting Network Traffic Data 用Network Traffic tool :收集传输数据
Collecting Network Traffic Data 1.This lesson teaches you to Tag Network Requests 标记网络类型 Configure a ...
- vue中数据接收成功,页面渲染失败
1.vue中数据接收成功,页面渲染失败.代码如下 经过查找资料修改代码为 或是 原因是: 由于 JavaScript 的限制, Vue 不能检测以下变动的数组: 当你利用索引直接设置一个项时,例如: ...
- 268 Missing Number 缺失的数字
给出一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数.案例 1输入: [3,0,1]输出: 2案例 2输入: [9,6,4,2,3,5,7, ...