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 分)——大树的遍历的更多相关文章

  1. PTA PAT排名汇总(25 分)

    PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...

  2. PAT Advanced 1138 Postorder Traversal (25) [树的遍历,前序中序转后序]

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

  3. PAT 1138 Postorder Traversal [比较]

    1138 Postorder Traversal (25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  4. A1138. Postorder Traversal

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

  5. PAT A1122 Hamiltonian Cycle (25 分)——图遍历

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  6. 1138. Postorder Traversal (25)

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

  7. PAT 1138 Postorder Traversal

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

  8. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  9. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

随机推荐

  1. 【高并发解决方案】7、一致性hash解读

       一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简 ...

  2. 洛谷P2178 [NOI2015]品酒大会(后缀自动机 线段树)

    题意 题目链接 Sol 说一个后缀自动机+线段树的无脑做法 首先建出SAM,然后对parent树进行dp,维护最大次大值,最小次小值 显然一个串能更新答案的区间是\([len_{fa_{x}} + 1 ...

  3. SEIG Modbus 3.4 CVE-2013-0662 漏洞分析与利用

    前言 Schneider Electric Modbus Serial Driver 会监听 27700 端口,程序在处理客户端发送的数据时会导致栈溢出. 测试环境: windows xp sp3 相 ...

  4. 《Inside C#》笔记(十三) 多线程 上

    通过将一个任务划分成多个任务分别在独立的线程执行可以更有效地利用处理器资源并节省时间.但如果不合理地使用多线程,反而会带来种种问题并拖慢运行速度. 一 线程基础 a)线程与多任务 一个线程就是一个处理 ...

  5. Django基础篇--用户权限管理和组管理

    Django作为一个成熟的python后台开发框架,为开发者提供了很多内置的功能,开发者只需要做一些配置就可以完成原生操作中比较复杂的代码编写.这些内置功能中其中一个比较强大的功能就是后台用户管理类. ...

  6. (网页)AngularJS中【Error: [$rootScope:inprog]】的解决办法(转)

    转自CSDN: Error: [$rootScope:inprog] http://errors.angularjs.org/1.5.8/$rootScope/inprog?p0=%24apply 如 ...

  7. vue-cli快速原型开发

    我们知道vue-cli提供了一套如何快速搭建vue开发脚手架的工具,虽然好用,但是有的时候我们还是嫌麻烦,因为就想快速开发调试一个组件,这时我们就可以使用vue-cli 3.x以上版本的一个好特性: ...

  8. 几种流行的AJAX框架jQuery,Mootools,Dojo,Ext JS的对比

    AJAX是web2.0的基石,现在网上流行几种开源的AJAX框架,比如:jQuery,Mootools,Dojo,Ext JS等等,那么我们到底在什么情况下该使用那个框架? 让我们来想想选择AJAX框 ...

  9. Linux : Vim 使用与配置 (附 GitHub 自动化配置脚本)

    由于经常使用 vim 编辑配置文件,有时候也会进行使用vim 编写一些脚本和c/c++ 程序,所以配置一个常用的 vim 是很是必要的.这篇博文主要是记录vim使用和配置相关的一些知识点. 关于vim ...

  10. Java之代理(jdk静态代理,jdk动态代理,cglib动态代理,aop,aspectj)

    一.概念 代理是什么呢?举个例子,一个公司是卖摄像头的,但公司不直接跟用户打交道,而是通过代理商跟用户打交道.如果:公司接口中有一个卖产品的方法,那么公司需要实现这个方法,而代理商也必须实现这个方法. ...