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 ...
随机推荐
- [dubbo] dubbo 基础使用
Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候 ...
- 5、Cocos2dx 3.0游戏开发找小三之測试例子简单介绍及小结
重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27186557 測试例子简单介绍 Cocos2d-x ...
- 超全面的JavaWeb笔记day18<事务&连接池&DBUtils>
1.事务 ACID 原子性 一致性 隔离性 持久性 mysql中开启和关闭事务 开启事务:START TRANSACTION 结束事务 提交事务:COMMIT 回滚事务:ROLLBACK JDBC中开 ...
- Effective C++ Item 19 Treat class design as type design
Too high class topic for me now ................... ................... ................... fill the ...
- POJ 1018 Communication System(树形DP)
Description We have received an order from Pizoor Communications Inc. for a special communication sy ...
- Loadrunner windows计数器
object (对象) Counters (计数器名称) Description (描述) 参考值 Memory Available Mbytes 可用物理内存数.如果该值很小(4MB或更小),则说明 ...
- Bitmap之安卓手机壁纸的设置
MainActivity: package com.hyzhou.imagedemo; import java.io.File; import android.os.Bundle; import an ...
- python2.0_s12_day13_javascript&Dom&jQuery
今天主要内容:JavaScriptDomjQuery http://www.cnblogs.com/wupeiqi/articles/5369773.html 今天主要内容大致了解:javascrip ...
- ajax 跨域访问 :Access-Control-Allow-Origin
一说到ajax跨域.首先想到的就是jsonp . JSONP方法是一种非官方方法,而且这种方法只支持GET方式,不如POST方式安全. 即使使用jQuery的jsonp方法,type设为POST,也 ...
- glassfish任意文件读取漏洞解析
一.背景: glassfish是一款java编写的跨平台的开源的应用服务器. 二.漏洞原理: 与宽字节SQL注入一致,都是由于unicode编码歧义导致的.具体payload如下构造: http:// ...