PAT 甲级 1138 Postorder Traversal
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的更多相关文章
- PAT Advanced 1138 Postorder Traversal (25) [树的遍历,前序中序转后序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and ...
- PAT 1138 Postorder Traversal [比较]
1138 Postorder Traversal (25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 1138 Postorder Traversal
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- 1138. Postorder Traversal (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- PAT甲级——A1138 Postorder Traversa【25】
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- 1138 Postorder Traversal
题意:给出二叉树的前序序列后中序序列,输出其后序序列的第一个值. 思路:乍一看不就是前序+中序重建二叉树,然后后序遍历嘛!这么做当然不会有错,但是却没有真正领会本题的意图.本题并不是让我们输出后序序列 ...
- PAT甲级目录
树(23) 备注 1004 Counting Leaves 1020 Tree Traversals 1043 Is It a Binary Search Tree 判断BST,BST的性质 ...
- PAT甲级 二叉树 相关题_C++题解
二叉树 PAT (Advanced Level) Practice 二叉树 相关题 目录 <算法笔记> 重点摘要 1020 Tree Traversals (25) 1086 Tree T ...
- PAT甲级1119. Pre- and Post-order Traversals
PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...
随机推荐
- sublime ruby [Decode error - output not utf-8] 错误
今天用sublime3 写ruby,然后最简单的 pust "hello" 都出不来, ctrl + b的时候报错.注:win7下 [Decode error - output n ...
- c++ 预处理和多重替换
预处理概念 #include #define extern 一. 预处理概念 在源代码编译成机器指令之前,都要进行预处理. 预处理阶段一般会在编译之前处理和修改C源代码.完成预处理后预 ...
- 学习OpenCV——SVM
学习OpenCV——SVM 学习SVM,首先通过http://zh.wikipedia.org/wiki/SVM, 再通过博客http://blog.csdn.net/yang_xian521/art ...
- 02-分页器,自定义分页器,解耦函数分页器,分页器class
1 .批量数据导入 主url from django.contrib import admin from django.urls import path, re_path, include urlpa ...
- 牛客网NOIP赛前集训营-提高组(第六场)B-选择题[背包]
题意 题目链接 分析 直接背包之后可以 \(O(n)\) 去除一个物品的影响. 注意特判 \([p==1]\) 的情况. 总时间复杂度为 \(O(n^2)\) . 代码 #include<bit ...
- pathon之多线程详解
一.线程理论 1.什么是线程 线程指的是一条流水线的工作过程 进程根本就不是一个执行单位,进程其实是一个资源单位--------将资源集合到一起: 一个进程内自带一个线程,线程才是CPU上的执行单位 ...
- NGUI可展开列表的实现
本文来自网易云社区 作者:汪毅军 最近使用了NGUI做了下可展开列表,其主要思路如下:首先最外层使用Scroll view以达到滑动效果,然后列表使用UITable进行排列,最后通过点击Item控制I ...
- 数据结构与算法 —— 链表linked list(06)
回文链表 链接 请检查一个链表是否为回文链表. 进阶:你能在 O(n) 的时间和 O(1) 的额外空间中做到吗? 解题思路: 回文链表的特点就是对称. 把链表放到栈中去,利用栈的先进后出的规则,和原链 ...
- .net 分布式学习计划
一: 1:.net分布式系统架构的思路 https://blog.csdn.net/slowlifes/article/details/53162014 2: nginx+iis实现负载均衡 ...
- net 快速打印日志
System.IO.File.AppendAllText(@"F:WriteText.txt", "日志内容“+"\r\n");