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. .net core中使用缓存(cache)

    官方文档:https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-2.2#use ...

  2. jstl c:if 不能判断成功的问题

    这是因为test里不能直接用 ${value}=='字符串' 的方式来进行判断比较,所以只能这样写 ${value == '字符串'} ,这样就能判断了

  3. Error:(1, 1) java: 非法字符: ‘\ufeff’

    一.问题 用IDEA打开eclipse java项目编译时,出现以下错误: Error:(1, 1) java: 非法字符: '\ufeff' Error:(1, 10) java: 需要class, ...

  4. 硬盘空间术语:unallocated, unused and reserved

    通过standard reports查看Disk Usage,选中Database,右击,选择Reports->Standard Reports->Disk Space Usage,截图如 ...

  5. python的变量的命名规则以及定义

    1.变量,指计算机中存储数据的空间 2.变量的命名方式:变量名 = 值 3.变量的命名规定(标识符的命名规定): 只能由数字,字母,下划线组成(可以用中文但是不推荐) 不能以数字开头 不能与关键词重名 ...

  6. Effective C++(Third Edition) Item29 为“异常安全”而努力是值得的

    “异常安全”有两个条件: 1.不泄露任何资源 可以通过以对象管理资源的方式(Item13). 2.不允许数据败坏 异常安全函数提供以下三种保证之一 a.基本承诺 如果异常被抛出,程序内的任何事物都仍然 ...

  7. Post请求和Get请求;@RequestBody和@RequestParam

    1.@RequestBody用于Post请求,接收json数据,例如:@RequestBody User user 例如:@RequestBody Map map .不要用于Get请求. 2.@Req ...

  8. Harbor 学习分享系列3 - Harbor用户指南

    云盘链接 链接:https://pan.baidu.com/s/1wvgI3KGGIckqzlkB-mYz4g 密码:doe7 通过本文无法把本文中的实验进行成功,请联系作者本人,作者会录制视频发送给 ...

  9. linq与lambda 常用查询语句写法对比

    LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量中被查询的值 [group by 条件] Lambda ...

  10. AJAX请求中出现OPTIONS请求

    背景 有一个前后端分离的VUE项目来发送ajax请求, 查看Nginx日志或使用Chrome Dev Tools查看请求发送情况时, 会看到每次调后台API的请求之前, 都会发送一个OPTIONS请求 ...