PAT A1138 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 <stdio.h>
#include <iostream>
const int maxn=;
struct node{
int data;
node* left,*right;
};
int in[maxn],pre[maxn];
node* create(int prel,int prer,int inl,int inr){
if(prel>prer) return NULL;
node* root = new node;
root->data = pre[prel];
int k;
for(k=inl;k<=inr;k++){
if(in[k]==pre[prel]) break;
}
int numleft = k-inl;
root->left = create(prel+,prel+numleft,inl,k-);
root->right = create(prel+numleft+,prer,k+,inr);
return root;
}
int main(){
int n;
scanf("%d",&n);
for(int i=;i<n;i++){
int d;
scanf("%d",&d);
pre[i]=d;
}
for(int i=;i<n;i++){
int d;
scanf("%d",&d);
in[i]=d;
}
int prel=,prer=n-,inl=,inr=n-;
int numleft=n;
int k;
while(numleft!=){
for(k=inl;k<=inr;k++){
if(in[k]==pre[prel]) break;
}
int nl=k-inl;
int nr=inr-k;
if(nl==){
prel=prel+nl+;
prer=prer;
inl=k+;
inr=inr;
numleft=nr;
}
else{
prel=prel+;
prer=prel+nl;
inl=inl;
inr=k-;
numleft=nl;
}
}
printf("%d",in[inl]);
}
注意点:这道题虽然给的时间很多650ms,但直接建树再查找还是会超时,好像可以用引用来避免超时。
不建树其实就是通过两个序列来找到第一个叶子节点,当一个节点的左边或右边只有一个元素时,剩下的那个元素就是要输出的值。
PAT A1138 Postorder Traversal (25 分)——大树的遍历的更多相关文章
- PTA PAT排名汇总(25 分)
PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...
- PAT Advanced 1138 Postorder Traversal (25) [树的遍历,前序中序转后序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and ...
- PAT 1138 Postorder Traversal [比较]
1138 Postorder Traversal (25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- A1138. Postorder Traversal
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- PAT A1122 Hamiltonian Cycle (25 分)——图遍历
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
- 1138. Postorder Traversal (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- PAT 1138 Postorder Traversal
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
随机推荐
- 关于Unsupported major.minor version 52.0解决办法(再次回顾)
对于web项目的配置问题,在很大程度上,tomcat的版本问题起到很大的决定性因素,例如以上问题:Unsupported major.minor version 52.0 表示stanford par ...
- C Looooops(poj2115+扩展欧几里德)
C Looooops Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Pr ...
- 设计模式之观察者模式(Observer)
观察者模式通常的叫法叫做订阅-发布模式,类似于报刊杂志的订阅,观察者和被观察者就是读者和邮局的关系,读者先要在邮局订阅想要的报刊,当报刊发行时,邮局会将报刊邮寄到读者家里.观察者(Observer)和 ...
- CSS属性disabled和readonly的区别是什么
在博客园中看到这样一篇文章,关于disabled和readonly的区别,以前还真的没有注意它们的区别,还是有必要知道它们的区别的,所以转载了. 这两个属性有类似之处,但是区别也是巨大的,之所以说类似 ...
- docker 部署 java 项目
Docker Docker官方网址: https://docs.docker.com/ 英文地址 Docker中文网址: http://www.docker.org.cn/ 中文地址 Docker是 ...
- Java并发编程(十二)Callable、Future和FutureTask
一.Callable与Runnable 先说一下java.lang.Runnable吧,它是一个接口,在它里面只声明了一个run()方法: public interface Runnable { pu ...
- servlet及jsp之间的请求转发
1.servlet间的请求转发 <dependencies> <dependency> <groupId>javax.servlet</groupId> ...
- 基于 node 搭建博客系统(一)
系统分为两端,分别实现. 管理员端: 功能 :个人信息,设置,发布随笔,随笔列表,删除随笔,查找,文章 等. 技术点:Boostrap + AdminLTE; 基于nodejs 实现的express ...
- Java获取数据库表 字段 存储的部分数据
在浏览器页面,选中图片(可多选) >单击删除按钮. 重点是, 本数据库表TabHeBeiTianQi中 存在 同一id,对应的picLocalPath字段 存储了多张图片,图片地址用 逗号 ...
- 第四章 Hyper-V 2012 R2 网络配置
尼玛的我不高兴写了,所以下面的文档我直接把原来的pdf给转换出来,加了点自己的注解,我写的话会写自己觉得终于的章节. 在搭建虚拟化平台时,网络的虚拟化是一个非常重要的环节,如何保障网络的持续可用并 ...