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 ...
随机推荐
- day02<Java语言基础+>
Java语言基础(常量的概述和使用) Java语言基础(进制概述和二,八,十六进制图解) Java语言基础(不同进制数据的表现形式) Java语言基础(任意进制到十进制的转换图解) Java语言基础( ...
- python2.0 s12 day8 _ socketserver学习
Socket 概念 一个socket就是一个点对点的链接.当今,大多数的通信都是基于Internet Protocl,因此大多数的网络Socket都是Internet Protocl(互联网)的通信( ...
- 上传绕过WAF的tips大全
原始默认状态: ——WebKitFormBoundary2smpsxFB3D0KbA7D Content-Disposition: form-data; name=”filepath”; filena ...
- linux系统socket通信编程详解函数
linux socket编程之TCP与UDP TCP与UDP区别 TCP---传输控制协议,提供的是面向连接.可靠的字节流服务.当客户和服务器彼此交换数据前,必须先在双方之间建立一个TCP连接,之 ...
- SDRAM容量的计算方法
当我在看有关SDRAM的芯片手册的时候,我一直在想这样一个问题:SDRAM的容量它究竟是如何计算的呢?经过查找网上的各种资料,最后在这里给大伙分享一下我的总结! 就以我的开发板的SDRAM的芯片手册的 ...
- JS中保留小数位数
一.1.2.toFixed(2)
- 《转》python学习(5)--数据类型
转自 http://www.cnblogs.com/BeginMan/archive/2013/06/08/3125876.html 一.标准类型函数 cmp():比较大小 str():转换为字符串 ...
- php array key 的存储规则
刚刚写程序遇到php数组取值的问题,发现字符串和数字取出来的是一样的. key 可以是 integer 或者string.value 可以是任意类型. 此外 key 会有如下的强制转换: 包含有合法整 ...
- 【BZOJ1529】[POI2005]ska Piggy banks Tarjan
[BZOJ1529][POI2005]ska Piggy banks Description Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个 ...
- shell出现syntax error near unexpected token `<' 解决方法
最新在看一个shell资料时,按照教材,却出现如下错误,不能运行 如下,简单的脚本: #!/bin/bash cat |while read line do echo $line done < ...