PAT 1138 Postorder Traversal
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
#include<iostream> //海星
#include<vector>
using namespace std;
vector<int> pre,in;
int flag=0;
void solve(int lb, int le, int rb, int re){
if(lb>le)
return ;
if(lb==le&&flag++==0)
cout<<pre[lb]<<endl;
int i=rb;
while(i<=re&&in[i]!=pre[lb]) i++;
solve(lb+1, lb+i-rb, rb, i-1);
solve(lb+i-rb+1, le, i+1, re);
}
int main(){
int n;
cin>>n;
pre.resize(n), in.resize(n);
for(int i=0; i<n; i++)
cin>>pre[i];
for(int i=0; i<n; i++)
cin>>in[i];
solve(0, n-1, 0, n-1);
return 0;
}
PAT 1138 Postorder Traversal的更多相关文章
- PAT 1138 Postorder Traversal [比较]
1138 Postorder Traversal (25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1138 Postorder Traversal
https://pintia.cn/problem-sets/994805342720868352/problems/994805345078067200 Suppose that all the k ...
- PAT Advanced 1138 Postorder Traversal (25) [树的遍历,前序中序转后序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and ...
- PAT A1138 Postorder Traversal (25 分)——大树的遍历
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- 1138. Postorder Traversal (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- 1138 Postorder Traversal
题意:给出二叉树的前序序列后中序序列,输出其后序序列的第一个值. 思路:乍一看不就是前序+中序重建二叉树,然后后序遍历嘛!这么做当然不会有错,但是却没有真正领会本题的意图.本题并不是让我们输出后序序列 ...
- PAT_A1138#Postorder Traversal
Source: PAT A1138 Postorder Traversal (25 分) Description: Suppose that all the keys in a binary tree ...
- [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 ...
随机推荐
- bzoj3262 陌上花开——CDQ分治
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3262 第一道CDQ分治题! 看博客:https://www.cnblogs.com/Narh ...
- Real-Time Compressive Tracking,实时压缩感知跟踪算法解读
这是Kaihua Zhang发表在ECCV2012的paper,文中提出了一种基于压缩感知(compressive sensing)的单目标跟踪算法,该算法利用满足压缩感知(compressive s ...
- json返序列化
ASP.NET中JSON的序列化和反序列化 http://www.cnblogs.com/zhaozhan/archive/2011/01/09/1931340.html 迟来的Json反序列化 ht ...
- $割点割顶tarjan$
原题 #include <bits/stdc++.h> using namespace std; typedef long long LL; inline LL read () { LL ...
- mutiset HDOJ 5349 MZL's simple problem
题目传送门 /* 这题可以用stl的mutiset容器方便求解,我对这东西不熟悉,TLE了几次,最后用读入外挂水过. 题解有O(n)的做法,还以为我是侥幸过的,后来才知道iterator it写在循环 ...
- 【LeetCode】LeetCode Weekly Contest 16B
2.一个游戏 YouTube MIT 算法课上有详细的讲解 思路是DP. 话说MIT OpenCourseWare 真心不错.应该好好看看,讲的详细生动. Tips: 1.当n是偶数的时候,首先的选择 ...
- 安装Kali里的应用程序或软件包
安装Kali里的应用程序或软件包 安装额外的软件是apt-get命令最基本的功能了,命令非常简单易懂.安装软件包的语法如下: apt-get install 软件包名 比如,安装图像编辑软件gimp, ...
- cocos creator 场景如何透明,多个canvas层级显示
转载地址:https://forum.cocos.com/t/creator-canvas/55373/14 Creator 版本:1.7 目标平台:WEB MOBILE 项目需要,页面做了多个Can ...
- Java 8 (10) CompletableFuture:组合式异步编程
随着多核处理器的出现,提升应用程序的处理速度最有效的方式就是可以编写出发挥多核能力的软件,我们已经可以通过切分大型的任务,让每个子任务并行运行,使用线程的方式,分支/合并框架(java 7) 和并行流 ...
- 全志A33平台编译linux(分色排版)sina33
全志A33平台编译linux 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/12 17:36 版本:V1.0 Xshell 5 (Buil ...