https://pintia.cn/problem-sets/994805342720868352/problems/994805345078067200

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 <bits/stdc++.h>
using namespace std; int n, pos;
vector<int> pre, in, post; void rec(int l, int r) {
if(l >= r) return;
int root = pre[pos ++];
int m = distance(in.begin(), find(in.begin(), in.end(), root));
rec(l, m);
rec(m + 1, r);
post.push_back(root);
} void solve() {
pos = 0;
rec(0, n);
printf("%d\n", post[0]);
} int main() {
scanf("%d", &n);
for(int i = 0; i < n; i ++) {
int k;
scanf("%d", &k);
pre.push_back(k);
}
for(int i = 0; i < n; i ++) {
int k;
scanf("%d", &k);
in.push_back(k);
}
solve();
return 0;
}

  FHFHFH

PAT 甲级 1138 Postorder Traversal的更多相关文章

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

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

  2. PAT 1138 Postorder Traversal [比较]

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

  3. PAT 1138 Postorder Traversal

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

  4. 1138. Postorder Traversal (25)

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

  5. PAT甲级——A1138 Postorder Traversa【25】

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

  6. 1138 Postorder Traversal

    题意:给出二叉树的前序序列后中序序列,输出其后序序列的第一个值. 思路:乍一看不就是前序+中序重建二叉树,然后后序遍历嘛!这么做当然不会有错,但是却没有真正领会本题的意图.本题并不是让我们输出后序序列 ...

  7. PAT甲级目录

    树(23) 备注 1004 Counting Leaves   1020 Tree Traversals   1043 Is It a Binary Search Tree 判断BST,BST的性质 ...

  8. PAT甲级 二叉树 相关题_C++题解

    二叉树 PAT (Advanced Level) Practice 二叉树 相关题 目录 <算法笔记> 重点摘要 1020 Tree Traversals (25) 1086 Tree T ...

  9. PAT甲级1119. Pre- and Post-order Traversals

    PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...

随机推荐

  1. Android开发——代码中实现WAP方式联网

    ,移动和联通的WAP代理服务器都是10.0.0.172,电信的WAP代理服务器是10.0.0.200. 在Android系统中,对于获取手机的APN设置,需要通过ContentProvider来进行数 ...

  2. Java集合——TreeMap源码详解

    )TreeMap 是一个有序的key-value集合,它是通过红黑树实现的.因为红黑树是平衡的二叉搜索树,所以其put(包含update操作).get.remove的时间复杂度都为log(n). (2 ...

  3. JavaScript总结(二)

    JavaScript的数据类型 ✍ 原始数据类型值: JavaScript有5中原始数据类型,通过调用typeof运算符返回值查看变量类型: ➣ Undefined ➔ 'undefined': Un ...

  4. 41-mysql作业

    1 2 3 4

  5. [BZOJ3745][COCI2015]Norma[分治]

    题意 题目链接 分析 考虑分治,记当前分治区间为 \(l,r\) . 枚举左端点,然后发现右端点无非三种情况: 极大极小值都在左边; 有一个在左边; 极大极小值都在右边; 考虑递推 \(l\) 的同时 ...

  6. pycharm如何显示工具栏

    1.没有工具栏的效果图如下: 2.在view中找到Toolbar打上勾即可显示: 3.工具栏设置成功显示效果图如下: 3.如何显示一个类或方法所在的文件,以及该文件下的所有方法,可以快速定位到该行

  7. 15-RUN vs CMD vs ENTRYPOINT

    RUN.CMD 和 ENTRYPOINT 这三个 Dockerfile 指令看上去很类似,很容易混淆.本节将通过实践详细讨论它们的区别. 简单的说: RUN 执行命令并创建新的镜像层,RUN 经常用于 ...

  8. 宝塔中mysql数据库命名小坑

    今天在通过宝塔新建网站,添加mysql数据库,名字中间有下划线,发现能够创建成功,但是实际链接后,是没有这个数据库的.是宝塔的原因还是liunx服务器的原因? 不支持下划线的数据库名字吗? 比如 bo ...

  9. springboot 前后端分离开发 从零到整(二、邮箱注册)

    spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 url: ...

  10. linux执行命令返回码释义

    Linux 操作系统错误代码解释 0.错误代码1-10 OS error code 0: Success 操作系统错误代码0:成功 OS error code 1: Operation not per ...