PAT 1138 Postorder Traversal [比较]

时间:2022-08-28 14:04:47
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 [比较]的更多相关文章

  1. PAT 1138 Postorder Traversal

    Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...

  2. PAT 甲级 1138 Postorder Traversal

    https://pintia.cn/problem-sets/994805342720868352/problems/994805345078067200 Suppose that all the k ...

  3. PAT Advanced 1138 Postorder Traversal &lpar;25&rpar; &lbrack;树的遍历,前序中序转后序&rsqb;

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and ...

  4. PAT A1138 Postorder Traversal (25 分)——大树的遍历

    Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...

  5. 1138&period; Postorder Traversal &lpar;25&rpar;

    Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...

  6. 1138 Postorder Traversal

    题意:给出二叉树的前序序列后中序序列,输出其后序序列的第一个值. 思路:乍一看不就是前序+中序重建二叉树,然后后序遍历嘛!这么做当然不会有错,但是却没有真正领会本题的意图.本题并不是让我们输出后序序列 ...

  7. PAT&lowbar;A1138&num;Postorder Traversal

    Source: PAT A1138 Postorder Traversal (25 分) Description: Suppose that all the keys in a binary tree ...

  8. &lbrack;LeetCode&rsqb; Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  9. &lbrack;LeetCode&rsqb; 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 ...

随机推荐

  1. mongodb的用户管理及安全认证

    1.确认mongodb的版本 > use admin switched to db admin > db.runCommand({}) { "version" : &q ...

  2. python绘图中使用公式时,解决&bsol;frac&lbrace;&rcub;&lbrace;&rcub;出来的字体太小的问题

    在用matplotlib绘图需要在图片中加入公式时,一般要用 text 或 annotate函数,并结合latex语法 '$...$'. 对于分数,如果直接使用\frac{}{},会造成分子分母上的字 ...

  3. 搭建vpn

    之前买的vpn,对linux支持很不友好,家里装的又是ubuntu.突然一想自己买个vps搭个vpn. 先买了host1plus的vps,一个月30块,配了两天,pptp,l2tp,shadow so ...

  4. setColorFilter&lpar;&rpar;滤镜

    ----------转载于:http://blog.sina.com.cn/s/blog_5da93c8f01012pkj.html 通过setColorFilter可以实现滤镜效果. 如:     ...

  5. eclipse设置自定义快捷键

    eclipse有很多强大且人性化的功能,而各项功能有时又隐藏得比较深(需要点击数次菜单才能找到),而系统提供的快捷键有时比较难记住甚至根本没有提供快捷键时,就需要自己手动设置快捷键了.设置方法有两种, ...

  6. sql中的触发器、视图、事务

    ·触发器(trigger) [触发器本质上还是一个存储过程,只不过不是用exe来调用执行,而是通过增删改数据库的操作] [触发器只对增.删.改有效] 触发器的格式 (instead of与for的区别 ...

  7. js实时监听input中值的变化

    $(function(){ $('#inputid').bind('input propertychange', function() { // input 中的值 var params = $(th ...

  8. Thrift compiler代码生成类解析

    代码生成类解析: Thrift--facebook RPC框架,介绍就不说了,百度,google一大把,使用也不介绍,直接上结构和分析吧. Hello.thrift文件内容如下: namespace ...

  9. jenkins的安装部署

    jenkins安装 参考连接: https://wiki.jenkins.io/display/JENKINS/Installing+Jenkins+on+Red+Hat+distributions ...

  10. java动态获取WebService的两种方式(复杂参数类型&rpar;

    java动态获取WebService的两种方式(复杂参数类型) 第一种: @Override public OrderSearchListRes searchOrderList(Order_Fligh ...