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. c++ 双向循环链表

    教学内容: 循环双链表 建立循环双链表 循环链表里插入结点 遍历循环链表 双向链表结构定义 struct stu_data { ];//学生名字 struct mytime stuTime;/ ...

  2. 10-[协程] greenlet模块、 gevent模块

    1.greenlet模块:实现20个任务切换 如果我们在单个线程内有20个任务,要想实现在多个任务之间切换,使用greenlet模块可以非常简单地实现这20个任务直接的切换 使用yield生成器的方式 ...

  3. idea maven javaweb项目迁移时的maven和版本报错问题解决(可解决同类错误)

    项目中代码红线报版本不支持xx语法,只需要将java版本设置为当前机器使用的java版本即可 这里我使用的是idea自带的maven,如果是自己安装的maven需要在 home directory 处 ...

  4. CSS中的height与line-height的区别

    <p class='text'>高与行高的区别</p> 那么我要想让这些字上下居中那么可以用宽度和行高控制 .text{ height:25px; line-height:25 ...

  5. Python 安装与专属 IDE_Pycharm 安装配置、永久激活,赠汉化版!

    这个为什么说是一次学生时代的经历呢,我的出发点并没有是为了吊胃口.确实,这个Python小应用,只能在学生时代用得着吧,尤其是高中和大学,如果你没有想到也没关系,看完我下面说的就会明白了. 对红蜘蛛软 ...

  6. Win10版本号区分

      版本号 内部版本号 UniversalApiContract  首个正式版 1507 10240 1  首个重大更新 1511 10586 2  一周年更新 Anniversary Update ...

  7. Android Studio 导入别人项目时候遇见的问题“Gradle DSL method not found: 'compile()'”

    Gradle DSL method not found: 'compile() 遇见这个问题截图: 解决: 在项目的根目录的build.gradle文件中是不是用了compile方法 如果有的话,剪切 ...

  8. elasticsearch(全文检索)的介绍和安装

    ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...

  9. Python模块搜索路径

    当一个名为 spam 的模块被导入的时候,解释器首先寻找具有该名称的内置模块.如果没有找到,然后解释器从 sys.path 变量给出的目录列表里寻找名为 spam.py 的文件.sys.path 初始 ...

  10. 三种UIScrollView嵌套实现方案

    背景 随着产品功能不断的迭代,总会有需求希望在保证不影响其他区域功能的前提下,在某一区域实现根据选择器切换不同的内容显示. 苹果并不推荐嵌套滚动视图,如果直接添加的话,就会出现下图这种情况,手势的冲突 ...