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的更多相关文章

  1. PAT 甲级 1020 Tree Traversals (二叉树遍历)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  2. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  3. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  4. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  5. PAT Advanced 1020 Tree Traversals (25 分)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  6. PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习

    1086 Tree Traversals Again (25分)   An inorder binary tree traversal can be implemented in a non-recu ...

  7. 【PAT】1020. Tree Traversals (25)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  8. PAT 甲级 1086 Tree Traversals Again

    https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 An inorder binary tree ...

  9. PAT甲级——A1086 Tree Traversals Again

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...

随机推荐

  1. C语言 编程练习22

    一.题目 1.编一个程序,输入x的值,按下列公式计算并输出y值: 2.已知数A与B,由键盘输入AB的值,交换它们的值,并输出. 3.给一个不多于5位的正整数,要求:一.求它是几位数,二.逆序打印出各位 ...

  2. C语言中的强制类型转换

    先直接放程序吧,后面还有总结. -------------------------------------------start------------------------------------ ...

  3. .Net 两个对像之间的映射 ( 二 )

    一.使用 class Program { static void Main(string[] args) { User u1 = new User(); u1.UserName = "aaa ...

  4. 20155233 《Java程序设计》 第十三周课堂练习总结

    20155233 <Java程序设计>第十三周课堂练习总结 测试题目 测试1 在IDEA中对P145 MathTool.java 使用JUnit进行单元测试,测试用例不少于三个,要包含正常 ...

  5. jdk和tomcat版本对应

    见tomcat的官网说明:tomcat.apache.org/whichversion.html Apache Tomcat ®是一个开源软件实现了Java Servlet和JavaServer Pa ...

  6. 【SDOI2014】数表

    题面 题解 不管$a$的限制 我们要求的东西是:($\sigma(x)$是$x$的约数个数和) $ \sum_{i=1}^n\sum_{j=1}^m\sigma(gcd(i,j)) $ 设$f(x)= ...

  7. 【BZOJ3142】[HNOI2013]数列

    [BZOJ3142][HNOI2013]数列 题面 洛谷 bzoj 题解 设第\(i\)天的股价为\(a_i\),记差分数组\(c_i=a_{i+1}-a_i\) 则 \[ Ans=\sum_{c_1 ...

  8. (转) 前端面试之js相关问题(一)

    原帖地址:http://stephenzhao.github.io/2016/08/19/Front-end-Job-Interview-Questions/ 最近我也是经历过面试别人和去面试的人了, ...

  9. Unity CombineTexture

    public Texture2D CombineTexture(Texture2D background, Texture2D top) { int width = background.width; ...

  10. 【转】自动化测试 - Appium + Python史上最全最简环境搭建步骤

    一,为什么是Appium借一张图: 1.1 Appium优点 l  开源 l  跨架构:NativeApp.Hybird App.Web App l  跨设备:Android.iOS.Firefox ...