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. [arc072F]Dam-[单调队列]

    Description 传送门 Solution 首先我们肯定不能那么耿直地直接把水混合起来吧..不然分分钟完球. 那么怎么找到最优解呢?假如我们把水的体积和温度按顺序插入队列,这时我们插入第i天的水 ...

  2. 8 stark组件 展示数据

    1.编辑按钮构建完成 1.必备知识预习 2.mark_safe模块:将html标签安全传输 3.构造表单数据 4.反向解析 https://www.cnblogs.com/yuanchenqi/art ...

  3. springmvc controller转发setViewName时找不到路径的问题以及转发视图时出现找不到样式的问题

    注释掉的部分是错误的写法,@RequestMapping 需要将方法放置在要转发的视图所在目录下,不然视图会找不到样式(无法正确的加载css文件), 如果将方法放在了视图所在目录下,那么 setUie ...

  4. SQL Server 日期格式和日期操作

    SQL Server发展至今,关于日期的格式的控制方法,有传统的方法,比如CONVERT(),也有比较便利的新方法,比如FORMAT():同样,关于日期的操作函数,也分为传统方法:DATEADD()等 ...

  5. 接口自动化学习--mock

    好久没有写学习的总结,都正月十二了,但还是要来个新年快乐鸭. 一直都在看imooc的一套java接口自动化实战课程,现在看到了尾部了,然后想到之前那些testng,mock,httpclient等都没 ...

  6. Charles工具内存不足时解决办法

    Charles runs out of memory After recording for a while Charles will run low on available memory. To ...

  7. python类与对象的组合与继承

    1.把类的对象实例化放到一个新的类里面叫做类的组合,组合就是指几个横向关系的类放在一起,纵向关系的类放在一起是继承,根据实际应用场景确定.简单的说,组合用于“有一个”的场景中,继承用于“是一个”的场景 ...

  8. Unity扩展编辑器三

    Scene视图是编辑游戏模型的地方,其实它还可以进行编辑,如下图所示,我给Scene视图做了简单的编辑 Scene视图的扩展是基于对象的,意思是你必须在Hierarchy视图中选择一个对象才行,Hie ...

  9. Unity Shader 学习之旅之SurfaceShader

    Unity Shader 学习之旅之SurfaceShader unity shader 图形图像  如果大地的每个角落都充满了光明 谁还需要星星,谁还会 在夜里凝望 寻找遥远的安慰——江河 官方文档 ...

  10. Siki_Unity_3-6_UI框架 (基于UGUI)

    Unity 3-6 UI框架 (基于UGUI) 任务1&2&3&4:介绍 && 创建工程 UI框架: 管理场景中所有UI面板 控制面板之间的跳转 如果没有UI框 ...