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 ...
随机推荐
- python3 class类 练习题
"""一.定义一个学生Student类.有下面的类属性:1 姓名 name2 年龄 age3 成绩 score(语文,数学,英语) [每课成绩的类型为整数] 类方法:1 ...
- 第一节 如何用Go实现单链表
一.概念介绍 下面这副图是我们单链表运煤车队. 每节运煤车就是单链表里的元素,每节车厢里的煤炭就是元素中保存的数据.前后车通过锁链相连,作为单链表运煤车,从1号车厢开始,每节车厢都知道后面拉着哪一节车 ...
- 20155226 2016-2017-2 《Java程序设计》第10周学习总结
20155226 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 程序员所作的事情 ...
- PGP的原理与使用
实验名称: PGP的原理与使用 姓名: 刘浩 20155307 任泓霖 20155319 班级: 1553 日期: 2017.10.16 一. 实验环境 Windows虚拟机下使用的是PKZIP2.0 ...
- # 第二周c实践所遇见的问题
第二周c实践所遇见的问题 地址符 在编程练习中时常忘记写入地址符,造成过运行错误,运行结果错误的惨痛教训,一个小小的错误耗费了很长的时间来寻找错误之处,养成写代码的一些好习惯势在必行.牢记scanf( ...
- Apache入门篇(四)之LAMP架构部署
一.LAMP解析 a: apachem: mariadb, mysqlp: php, perl, python 静态资源:静态内容:客户端从服务器获得的资源的表现形式与原文件相同:动态资源:通常是程序 ...
- NB-IOT模组指令AT+NMSTATUS和AT+CGPADDR对比
1. AT+NMSTATUS,这个指令是用来查询模块在IOT平台的注册情况.注册指的是lwm2m协议里面的注册机制,详细可以参考lwm2m协议. 2. AT+MREGSWT,设置重启之后,自动启动注册 ...
- cogs1341 永无乡
cogs1341 永无乡 打了一发替罪羊树. 鬼故事:替罪羊树去掉重构(变成裸的二叉排序树)依然跑得过= = 启发式合并.每次把小的里面所有东西往大的里面一丢,每个点最多被丢\(log_2n\)次(丢 ...
- vue-cli的安装使用
通过 npm install vue 安装vue 后: 1.打开cmd 输入 npm install --global vue-cli 全局安装vue-cli脚手架 输入 vue -V 返回vue-c ...
- 机器学习与R语言——基本语法
一.注释 1.选中要注释的内容,快捷键Ctrl+Shift+C(取消注释方法相同) 2.在需注释内容前输入# #需注释的内容 3.利用if语句,将判断条件设置为false则可跳过if语句中的内容,相当 ...