题目

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 (<=50000), 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

题目分析

已知前序和中序,求后序第一个节点值

解题思路

  1. postFirst函数本质是建树过程,找到后序遍历的第一个节点后退出以节省时间(后序第一个节点为递归过程中第一个左子节点和右子节点都为NULL的节点)

易错点

该题节点数最多为50000个,若在找到后没有退出(即:完成建树过程)会超时

Code

Code 01

#include <iostream>
#include <vector>
const int maxn = 50000;
struct node {
int data;
node * left;
node * right;
};
int n,pre[maxn],in[maxn];
bool flag;
void postFirst(int inL,int inR, int preL,int preR) {
if(inL>inR||flag) return;
int k=inL;
while(k<inR&&in[k]!=pre[preL])k++;
postFirst(inL, k-1, preL+1, preL+(k-inL));
postFirst(k+1, inR, preL+(k-inL)+1, preR);
if(flag==false) {
printf("%d", pre[preL]);
flag=true;
}
}
int main(int argc,char * argv[]) {
scanf("%d",&n);
for(int i=0; i<n; i++)scanf("%d",&pre[i]);
for(int i=0; i<n; i++)scanf("%d",&in[i]);
postFirst(0,n-1,0,n-1);
}

PAT Advanced 1138 Postorder Traversal (25) [树的遍历,前序中序转后序]的更多相关文章

  1. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

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

  2. PAT 甲级 1138 Postorder Traversal

    https://pintia.cn/problem-sets/994805342720868352/problems/994805345078067200 Suppose that all the k ...

  3. 1138. Postorder Traversal (25)

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

  4. PAT Advanced A1021 Deepest Root (25) [图的遍历,DFS,计算连通分量的个数,BFS,并查集]

    题目 A graph which is connected and acyclic can be considered a tree. The height of the tree depends o ...

  5. PAT 1138 Postorder Traversal [比较]

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

  6. PAT Advanced 1102 Invert a Binary Tree (25) [树的遍历]

    题目 The following is from Max Howell @twitter: Google: 90% of our engineers use the sofware you wrote ...

  7. PAT (Advanced Level) 1136~1139:1136模拟 1137模拟 1138 前序中序求后序 1139模拟

    1136 A Delayed Palindrome(20 分) 题意:给定字符串A,判断A是否是回文串.若不是,则将A反转得到B,A和B相加得C,若C是回文串,则A被称为a delayed palin ...

  8. 【构建二叉树】02根据中序和后序序列构造二叉树【Construct Binary Tree from Inorder and Postorder Traversal】

    我们都知道,已知中序和后序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 中序遍历序列,           ======O=========== 后序遍 ...

  9. [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

随机推荐

  1. C++寒假作业2

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzu/2020OOP 这个作业要求在哪里 https://edu.cnblogs.com/campus/fzu/2 ...

  2. JAVA - 创建SpringBoot项目

    JAVA - 创建SpringBoot项目 Spring Boot是由Pivotal团队提供的全新框架,设计目的是用来简化新Spring应用的初始搭建以及开发过程.它主要推崇的是'消灭配置’,实现零配 ...

  3. 箭头函数this

    箭头函数的this值是由包含它的函数(非箭头函数)来决定的,与包含的函数的this指向一致,如果包裹它的不是函数(直到找到最外层)则this指向全局对象 并且箭头函数的this是固定的,由定义它时所在 ...

  4. ACM-Maximum Tape Utilization Ratio

    题目描述:Maximum Tape Utilization Ratio Tags: 贪婪策略 设有n 个程序{1,2,…, n }要存放在长度为L的磁带上.程序i存放在磁带上的长度是li ,1 < ...

  5. [BJDCTF2020]ZJCTF,不过如此

    0x00 知识点 本地文件包含伪协议 ?text=php://input //执行 post: I have a dream ?file=php://filter/read/convert.base6 ...

  6. 在执行 php artisan key:generate ,报 Could not open input file: artisan 错误

    Could not open input file: artisan 必须保证命令是在项目根目录,如下图所示:

  7. ORACLE 将一个库的部分值带条件插入到另外一个库

    将一个表插入另外一个表,两种方法: 1.insert into table1 select * from table2 ; 或者2.create table1 as select * from tab ...

  8. C语言-浮点类型

    C语言-浮点类型 浮点类型 在0的两侧有一小块区域,这个区域非常接近0,但是不等于0,是float(表达范围数量级10^-38^)或者double(达范围数量级10^-308^)无法表达的,而0是可以 ...

  9. springboot自动装配介绍

    所谓的自动装配,就是 autowire. 如何激活自动装配呢? 方法一:@EnableAutoConfiguration或@SpringBootApplication,写在@Configuration ...

  10. springboot~不用模板执行html文件

    放到在resources/static目录下,创建目录或html文件,均可.如: