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,修改之后第一个代码也过了,说明运行超时不是函数传参参数个数的问题。

最新文章

  1. vue-router(2.0)
  2. visudo 使用摘记
  3. 【SSM 2】spring常用注解
  4. Python socket编程之四:模拟分时图
  5. 用Java操作树莓派!pi4j简介与安装
  6. CSS控制checkbox样式
  7. java BigDecimal的操作
  8. C#二维码生成与解码
  9. 9.Smarty的循环
  10. LINUX 笔记-DU 和 DF
  11. class not found: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  12. git冲突时解决方法
  13. 2017-10-6模拟赛T1 手工(handicraft.*)
  14. 开源小程序CMS网站, JeeWx-App-CMS 1.1 版本升级发布,持续更新!
  15. Ice_cream&#39;s world I(并查集成环)
  16. Spark调优_性能调优(一)
  17. c++自制锁机程序--两行代码
  18. Python 读、写、追加csv文件详细以及注意事项
  19. 【杂谈】对RMI(Remote Method Invoke)的认识
  20. [转]IDEA 新建 JSP 项目时

热门文章

  1. 如何将ppt转换为高清图片?
  2. POJ 1837 Balance(01背包变形, 枚举DP)
  3. swift--动画效果
  4. python2.0_s12_day9_协程&amp;多线程和cpu,磁盘io之间的关系
  5. conn.setAutoCommit(false)数据回滚设置
  6. 《C++ Primer Plus》第12章 类和动态内存分配 学习笔记
  7. x64dbg使用心得
  8. Js跨域、父级窗口执行JS赋值、取值,更改元素
  9. web基础-----&gt;模板引擎Velocity的使用(一)
  10. Android Training - 管理应用的内存