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 (25) [树的遍历,前序中序转后序]

    题目 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. Postorder Traversal (25)

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

  6. 1138 Postorder Traversal

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

  7. PAT_A1138#Postorder Traversal

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

  8. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

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

  9. [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 ...

随机推荐

  1. angularJs 页面{{xxx}}使用三目运算符

    <td>{{::item.sex=='w'?'女':'男'}}</td>,记得引号.也可以不用::,用不用::的区别,自行百度

  2. 第四章 Spring.Net 如何管理您的类___对象的生命周期链

    各位,实在不好意思,因为有事,博客几天没更新了.前面有童鞋提到,配置 Objects.xml 路径的相关问题,这些东西是 IResource 接口的一些内容.在下一章会详细介绍. 传统的Net应用中, ...

  3. swift - UIToolbar 的用法

    代码如下: 1.声明及初始化 var toolsBar = UIToolbar() toolsBar.frame = CGRect(x:, y:, width:SCREEN_WIDTH, height ...

  4. mysql中根据一个字段相同记录写递增序号,如序号结果,如何实现?

      mysql中根据一个字段相同记录写递增序号,如序号结果,如何实现? mysql中实现方式如下: select merchantId, NameCn, send_date, deliver_name ...

  5. laravel 使用 vue (gulp)

    1)首先要安装 gulp 看这里 http://www.cnblogs.com/tujia/p/6397779.html 2)编辑js 默认 laravel 里有一个 /resources/asset ...

  6. UE4 Run On Server与Run on owning client

  7. Websphere停止服务不用输入账号密码

    启用了安全性的WebSphere Application Server,在日常维护中经常在停止服务的时候需要输入用户名和密码.停止的方式如下:[root@was /]# /opt/IBM/WebSph ...

  8. Android SDK更新下载失败以及Studio首次安装取消自动下载SDK

    这是因为,此时Android Studio会去获取 android sdk 组件信息,这个过程相当慢,还经常加载失败,导致Android Studio启动不起开. 解决办法: 不去获取android ...

  9. Js debug模式

    在代码中需要调试的地方,输入“debugger;”:

  10. OracleClient安装系统环境变量配置

    1.变量名:TNS_ADMIN  值:E:\instantclient_11_2\NETWORK\ADMIN 2.变量名:NLS_LANG    值:SIMPLIFIED CHINESE_CHINA. ...