PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
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
题意:
后序中序链表建树,求层序
AC代码:
#include<bits/stdc++.h>
using namespace std;
int n;
struct node{
int data;
node *left,*right;
};
int post[];
int in[];
vector<int>level;
queue<node*>q;
node *build(int pl,int pr,int il,int ir){
if(pl>pr || il>ir) return NULL;
int pos=-;
for(int i=il;i<=ir;i++){
if(in[i]==post[pr]){
pos=i;
break;
}
}
node *root=new node();
root->data=post[pr];
root->right=build(pr-(ir-pos),pr-,pos+,ir);//是ir-pos
root->left=build(pl,pr-(ir-pos)-,il,pos-);
return root;
}
int main(){
cin>>n;
for(int i=;i<=n;i++) cin>>post[i];
for(int i=;i<=n;i++) cin>>in[i];
node *root=build(,n,,n);
q.push(root);
while(!q.empty()){
root=q.front();
level.push_back(root->data);
q.pop();
if(root->left) q.push(root->left);
if(root->right) q.push(root->right);
}
for(int i=;i<level.size();i++){
cout<<level.at(i);
if(i!=level.size()-) cout<<" ";
}
return ;
}
PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习的更多相关文章
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT Advanced 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 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- PAT 甲级 1020 Tree Traversals
https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...
- 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)
题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...
- PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- 1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 1020 Tree Traversals (25分)思路分析 + 满分代码
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
随机推荐
- 大数据调度工具oozie详细介绍
背景 之前项目中的sqoop等离线数据迁移job都是利用shell脚本通过crontab进行定时执行,这样实现的话比较简单,但是随着多个job复杂度的提升,无论是协调工作还是任务监控都变得麻烦,我们选 ...
- maven 下载 jar 包速度慢时,可以自己手动下载 jar 包,然后粘贴到 External Libraries 中
maven 下载 jar 包速度慢时,可以自己手动下载 jar 包,然后粘贴到 External Libraries 中
- keywordAsVar.php
<?php //keywordAsVar.php #keywordAsVar.php $True="我是变量True"; echo($True); echo("&l ...
- vector rIterator
#include<vector> #include<iostream> using namespace std; void main() { vector<int> ...
- MySQL - binlog日志简介及设置
基本概念 binlog是Mysql sever层维护的一种二进制日志,与innodb引擎中的redo/undo log是完全不同的日志:其主要是用来记录对mysql数据更新或潜在发生更新的SQL语句, ...
- 【每天学一点Linux】centos7 docker 启动cpu100% 飙升居高不下 无法关机 无法杀死进程
目前不知道什么原因. 重装了docker后仍然不行.安装方式为yum(在线和本地方式). 后来使用了下载static压缩包的方式来使用,就没有再出现如题的问题了.安装包地址为:https://down ...
- ftp使用
1.pom文件中添加依赖 <!-- ftp使用 --><dependency> <groupId>commons-net</groupId> <a ...
- Axure9 9.0.0.3648 秘钥亲测可用
用户名: thisProEdition秘钥:PkgeOGle9dSCMXTAczSrTGQZJLcviUJO1kG6onDIH/C68b+AUItWKdbBrmkJsJ0m
- Content-type解析
一.是什么? 是Http的实体首部字段,用于说明请求或返回的消息主体是用何种方式编码,在request header和response header里都存在. Content-Type(内容类型),一 ...
- 迁移学习、fine-tune和局部参数恢复
参考:迁移学习——Fine-tune 一.迁移学习 就是把已训练好的模型参数迁移到新的模型来帮助新模型训练. 模型的训练与预测: 深度学习的模型可以划分为 训练 和 预测 两个阶段. 训练 分为两种策 ...