PAT 1138 Postorder Traversal [比较]
1138 Postorder Traversal (25 分)
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>
#include<cstdio>
using namespace std; vector<int> pre,in,post;
void getPost(int inL,int inR,int preL,int preR){
if(inL>inR||post.size()!=)return ;
//if(preL>preR)return ;
int i=;
while(in[i]!=pre[preL])i++;
getPost(inL,i-,preL+,preL+i-inL);
getPost(i+,inR,preL+i-inL+,preR);
post.push_back(pre[preL]);
}
int main() {
int n;
cin>>n;
int t;
for(int i=;i<n;i++){
cin>>t;
pre.push_back(t);
}
for(int i=;i<n;i++){
cin>>t;
in.push_back(t);
}
getPost(,n-,,n-);
cout<<post[];
return ;
}
//这样写取判断总有最后两个或者倒数第二个测试点过不去,运行超时。
//尝试想传递&引用,发现不行,报错:
\main.cpp||error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'|
改成以下之后,也就是将函数参数减少一个:
#include <iostream>
#include <vector>
#include<cstdio>
using namespace std; vector<int> pre,in,post;
void getPost(int inL,int inR,int pr){
if(inL>inR||post.size()>)return ;
//if(preL>preR)return ;
int i=;
while(in[i]!=pre[pr])i++;
getPost(inL,i-,pr+);
getPost(i+,inR,pr+i-inL+);
post.push_back(pre[pr]);
}
int main() {
int n;
cin>>n;
int t;
for(int i=;i<n;i++){
cin>>t;
pre.push_back(t);
}
for(int i=;i<n;i++){
cin>>t;
in.push_back(t);
}
getPost(,n-,);
cout<<post[];
return ;
}
倒数第二个测试点超时。
//忽然想起,将i初始化时改为inL,然后就AC了!!!这样遍历的就少了。
主要问题就是i的初始化问题,一定要初始化为inL,修改之后第一个代码也过了,说明运行超时不是函数传参参数个数的问题。
PAT 1138 Postorder Traversal [比较]的更多相关文章
- PAT 1138 Postorder Traversal
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- 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 ...
随机推荐
- linux 数据盘和系统盘的查看
系统盘就像linux的c盘,使用df -l命令查看 如下所示: 可以看到根路径 / 都是位于系统盘.而/root,/home,/usr就如同c盘下的c:\windows,c:\usr这些目录 如果单独 ...
- Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(二)
接前一篇 Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 本篇主要内容:Spring Type Conver ...
- Environment.GetEnvironmentVariable
参考: https://jingyan.baidu.com/article/b24f6c82cba6dc86bfe5da9f.html https://msdn.microsoft.com/zh-cn ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第14章节--使用Office Services开发应用程序 WOPI和新的Office Web Apps Server
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第14章节--使用Office Services开发应用程序 WOPI和新的Office Web Apps Server ...
- ScrollView:ScrollView can host only one direct child异常
java.lang.IllegalStateException: ScrollView can host only one direct child 原因是在外面有一个TextView控件,将其删除则 ...
- BSTR与char*、cstring、CComBSTR的转换
// BSTR_Convert.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <comutil.h> // ...
- Mybatis中的if test 标签
<if test='patrolResult != null and patrolResult != "" and patrolResult !="1"' ...
- Windows下Mysql主从配置(Mysql5.5)
主数据库IP:192.168.3.169从数据库IP:192.168.3.34 主数据库配置my.inin: 在[mysqld]下添加配置数据:server-id=1 #配一个唯一的ID编号, ...
- 如何打开或关闭windows的测试模式
百度经验:jingyan.baidu.com windows的测试模式就如同字面意思一样,是一个测试用的模式.这个模式的标志主要在有非官方驱动或系统关键文件运行时显示.例如安装了大内存补丁(32位系统 ...
- php数据访问之查询关键字
本文根据数据库中的car表做一个汽车查询页面,巩固php查询关键字操作,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了php查询操作的实现代码,供大家参考,具体内容如下 一.一个关键字查询 主 ...