PAT 甲级 1020 Tree Traversals
https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order 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 (≤), the total number of nodes in the binary tree. The second line gives the postorder 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 level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2
代码:
#include <bits/stdc++.h>
using namespace std; int N;
vector<int> in, post, level(100000, -1); void rec(int root, int st, int en, int index) {
if(st > en) return;
int i = st;
while(i < en && in[i] != post[root]) i ++;
level[index] = post[root];
rec(root - 1 - en + i, st, i - 1, 2 * index + 1);
rec(root - 1, i + 1, en, 2 * index + 2);
} int main() {
scanf("%d", &N);
in.resize(N), post.resize(N);
for(int i = 0; i < N; i ++)
scanf("%d", &post[i]);
for(int i = 0; i < N; i ++)
scanf("%d", &in[i]); rec(N - 1, 0, N - 1, 0);
int cnt = 0;
for(int i = 0; i < level.size(); i ++) {
if(level[i] != -1) {
if(cnt) printf(" ");
printf("%d", level[i]);
cnt ++;
}
if(cnt == N) break;
}
return 0;
}
已知后序中序求层序遍历的结果
vector<int> level(100000, -1); 这句话很有必要
PAT 甲级 1020 Tree Traversals的更多相关文章
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- 【PAT】1020. Tree Traversals (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- PAT 甲级 1086 Tree Traversals Again
https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 An inorder binary tree ...
- PAT甲级——A1086 Tree Traversals Again
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...
随机推荐
- ACM1021:Fibonacci Again
Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) ...
- svn文件管理
将VS2010工程提交给Git管理时需要哪些文件: *.h *.cpp *.sln *.vcxproj *.vcxproj.filters *.qrc以及Resources目录下的资源 ...
- Swift里performSelector方法的替代
最近在回答StackOverflow的问题时,发现performSelector方法在Swift被去掉,Apple的注释是这个方法被去掉是因为不安全: NOTE The performSelector ...
- 20155319 2017-2018-1《信息安全系统设计》第四周课堂测试、Makefile、myod
20155319 2017-2018-1<信息安全系统设计>第四周课堂测试.Makefile.myod 测试2-gcc测试 1.用gcc 进行预处理,编译,汇编,链接vi输入的代码 2.生 ...
- Ubuntu genymotion
官网注册帐号 下载genymotion-[VERSION]_[ARCH].bin 进入android studio In Android Studio, go to File > Setting ...
- eclipse 打包maven项目的坑
一.问题: 公司开发了一个项目,需要作为后台服务运行,整个项目的构成是:[maven + spring + eclipse] 在使用打包的时候遇到许多问题: (1)eclipse中maven工具的集成 ...
- PyQt5 结合 matplotlib 时,如何显示其 NavigationToolbar
本文目的:展示 PyQt5 结合 matplotlib 时,如何显示其 NavigationToolbar. 本人搜遍所有网络无果,没办法,查看PyQt5源代码,最终才搞明白...特此留记. 〇.Py ...
- SRM 698 div1 RepeatString
250pts RepeatString 题意:问最少修改多少次将一个字符串修改为AA的形式.可以插入一个字符,删除一个字符,修改字符. 思路:枚举分界点,然后dp一下. /* * @Author: m ...
- Unity商店下载的文件保存路径?
Win7系统: C:\Users\系统用户名\AppData\Roaming\Unity\Asset Store MAC:"~/Library/Unity/Asset\ Store" ...
- scikit-learn API
scikit-learn API 这是scikit-learn的类和函数参考.有关详细信息,请参阅完整的用户指南,因为类和功能原始规格可能不足以提供有关其用途的完整指南. sklearn.base:基 ...