PAT_A1138#Postorder Traversal
Source:
Description:
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and inorder traversal sequences, you are supposed to output the first number of the postorder traversal sequence of the corresponding binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 50,000), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the first number of the postorder traversal sequence of the corresponding binary tree.
Sample Input:
7
1 2 3 4 5 6 7
2 3 1 5 4 7 6
Sample Output:
3
Keys:
- 二叉树的遍历
Attention:
- 建树的过程实质上也是遍历二叉树的过程
Code:
/*
Data: 2019-05-26 20:53:20
Problem: PAT_A1138#Postorder Traversal
AC: 16:20 题目大意:
假设二叉树中各个结点权值不同且为正;
给出先序和中序遍历,求后序遍历中的第一个结点
*/ #include<cstdio>
const int M=1e5;
int pre[M],in[M],f=; void Travel(int preL, int preR, int inL, int inR)
{
if(preL > preR)
return;
int k;
for(k=inL; k<=inR; k++)
if(in[k]==pre[preL])
break;
int numLeft = k-inL;
Travel(preL+, preL+numLeft, inL,k-);
Travel(preL+numLeft+, preR, k+,inR);
if(f){
printf("%d\n", in[k]);f=;
}
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n;
scanf("%d", &n);
for(int i=; i<n; i++)
scanf("%d", &pre[i]);
for(int i=; i<n; i++)
scanf("%d", &in[i]);
Travel(,n-,,n-); return ;
}
PAT_A1138#Postorder Traversal的更多相关文章
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- Leetcode Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- Leetcode Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
随机推荐
- [bzoj1040][ZJOI2008]骑士_树形dp_基环树_并查集
骑士 bzoj-1040 ZJOI-2008 题目大意:n个骑士,每个骑士有权值val和一个讨厌的骑士.如果一个骑士讨厌另一个骑士那么他们将不会一起出战.问出战的骑士最大atk是多少. 注释:$1\l ...
- ZooKeeper官方文档资源
一般来说官方的文档是最权威的. 入口:http://zookeeper.apache.org/ 在右侧即可进入相应版本文档: 如果想要看主干的文章,入口如下,主干是最稳当的版本:http://zook ...
- ZooKeeper之初识
它是什么 俗称动物管理员,它使用java开发,开源,接口简单,高效,稳定的分布式系统,为其它分布式系统提供协调服务 为什么会存在? 开发分布式系统跟单机上做开发完全不同,碰到的问题完全不同,开发分布式 ...
- Verilog与SystemVerilog编程陷阱:怎样避免101个常犯的编码错误
这篇是计算机类的优质预售推荐>>>><Verilog与SystemVerilog编程陷阱:怎样避免101个常犯的编码错误> 编辑推荐 纠错式学习,从"陷阱 ...
- android 添加一个按键键值【转】
本文转载自:http://blog.csdn.net/u012719256/article/details/52526046 1.frameworks/base/data/keyboards/Gene ...
- System.IO.Path 操作
System.IO.Path 分类: C#2011-03-23 10:54 1073人阅读 评论(0) 收藏 举报 扩展磁盘string2010c System.IO.Path提供了一些处理文件名和路 ...
- bzoj3033 太鼓达人——欧拉图搜索
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3033 考虑那 (1<<k) 个数,要形成答案,必然是相邻两个数间有 k-1 个重 ...
- 实现泛型IEnumerable接口
用C#实现一个类的IEnumerable接口时有两种方法:1)实现非泛型IEnumerable接口:2)实现泛型IEnumerable(T)接口.如果采用方法1,当集合元素T是值类型时,将涉及到巨多的 ...
- Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements 开始想写一个 ...
- z-index 、层叠上下文、层叠级别、z-index失效
一.z-index z-index默认处于非激活状态,只有定位元素(即position:relative/absolute/fixed时)才会被激活. z-index与层叠上下文关联. 当z-inde ...