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. Java 图形化界面设计(GUI)实战练习(代码)

    关于Java图形化界面设计,基础知识网上可搜,下面简单介绍一下重点概念,然后就由浅入深代码实例. 程序是为了方便用户使用的,Java引入图形化界面编程. 1.JFrame 是容器类 2.AWT 是抽象 ...

  2. csharp: mappings using Dapper-Extensions+Dapper.net.

    sql: CREATE TABLE [PotoUsers] ( [UserID] INT IDENTITY(1,1) PRIMARY KEY, [UserName] NVARCHAR(50), [Fi ...

  3. CSS外观属性

    CSS外观属性 color 文本颜色 line-height 行间距 text-aline水平对齐方式 text-indent 首行缩进 letter-spacing字间距 word-spacing ...

  4. CSS之fontAwesome代替网页icon小图标

    引言 奥森图标(Font Awesome)提供丰富的矢量字体图标—通过CSS可以任意控制所有图标的大小 ,颜色,阴影. 网页小图标到处可见,如果一个网页都是干巴巴的文字和图片,而没有小图标,会显得非常 ...

  5. js 中判断变量是数组还是对象,和判断对象是否为空

    判断是对象还是数组  var ids={ id:'1',num:'2' } if(Array.isArray(ids) == false) {console.log('不是数组,对象') } else ...

  6. ArcGIS Server Rest 认证过程分析

    1. http://192.168.1.220:6080/arcgis/admin/login?redirect= Request URL: http://192.168.1.220:6080/arc ...

  7. SGCC_UAP启动停留在initializing java tooling(1%)

    找到uap的安装目录,eclipse文件夹下的eclipse.ini,用EditPlus打开,添加下面两行 -vmC:\Program Files\Java\jdk1.6.0_43\bin\ 在-vm ...

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

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

  9. Spark Word2Vec算法代码实现

    1 import com.hankcs.hanlp.tokenizer.NLPTokenizer import org.apache.hadoop.io.{LongWritable, Text} im ...

  10. NoSQL&Redis

    1.介绍NoSQL NoSQL(Not Only SQL):不仅仅是SQL,是一项全新的数据库理念,泛指非关系型数据库,原来我们所使用的MySQL.Oracle.Microsoft SQL Serve ...