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 ...
随机推荐
- Executors 挖坑
Executors 挖坑 线程频繁的创建销毁是有代价的,所以Java为我们提供了线程池 线程池构造方法很多 我们一般使用Executors的工厂方法: public static ExecutorSe ...
- C++ —— 类中static和const关键字声明变量的初始化方式总结
在类中声明变量/常量时,经常会用到static.const关键字.对于该变/常量的初始化问题,网上有许多相关文章,但是大多不够完善,或者存在错误.经过实际验证,总结如下: (注明:测试编译平台为VS2 ...
- C++基本概念复习
照着https://www.w3cschool.cn/cpp/,把C++的基础概念顺了一遍,虽然很久没用C++,还是整理一下. #include "stdafx.h"; #incl ...
- Oracle入门第二天(上)——基本查询SQL
一.SQL概述 起源于标准不再赘述,主要分为DDL,DML,DCL 相关介绍,参考MySQL章节:http://www.cnblogs.com/jiangbei/p/6696202.html 二.基本 ...
- 20155302 2016-2017-2 《Java程序设计》 第1周学习总结
20155302 2016-2017-2 <Java程序设计> 第1周学习总结 教材学习内容总结 浏览全书提出问题 chapter1:怎么保证现在系统在用最高版本的JRE呢?在哪里查看及升 ...
- android studio提交到开源git时出现:fatal: refusing to merge unrelated histories的解决办法
创建本地库和fetch远程分支这些前面的步骤这里略过.可以自行百度. 解决办法: 1.cmd进入项目的根目录. 2.执行下面的命令:git pull origin master --allow-unr ...
- vue 3.0使用 BUG解决
最近在做vue的项目,吊进了很多坑,这些坑很浅,但一旦掉进去了,不花点功夫还爬不出来.所以总结下,当做下笔记,持续更新 1.<router-link> 里加的事件没反应 错误代码: < ...
- macOS 10.14 Mojave Apache设置:多个PHP版本
[18/6/2018更新]由于Homebrew/php点击在2018年3月底被弃用,并将所有PHP公式移动到Homebrew/core,我们已经重新设计了我们的指南,使用这个新的水龙头. 如果您过去一 ...
- 二、Web框架实现
一.简单web(socket) 在前一篇WEB框架概述一文中已经详细了解了:从浏览器键入一个URL到返回HTML内容的整个过程.说到底,本质上其实就是一个socket服务端,用户的浏览器其实就是一个s ...
- javaweb(十一)——使用Cookie进行会话管理
一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...