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 ...
随机推荐
- iView学习笔记(二):Table行编辑操作
1.前端准备工作 首先新建一个项目,然后引入iView插件,配置好router npm安装iView npm install iview --save cnpm install iview --sav ...
- php的选择排序
往前. <?php /** * 选择排序 * 工作原理是每次从待排序的元素中的第一个元素设置为最小值, * 遍历每一个没有排序过的元素,如果元素小于现在的最小值, * 就将这个元素设置成为最小值 ...
- django常用命令行和一些笔记
命令行 新建项目:django-admin startproject projectname 新建应用:python manage.py startapp appname(每次创建了新的app后,都需 ...
- url 组成
- TabBar 设置可滚动:isScrollable: true
appBar: AppBar( bottom: TabBar( // 设置可滚动 isScrollable: true, controller: _tabController, tabs: tabs. ...
- react hooks沉思录
将UI组件抽象为状态处理机.分为普通状态和副作用状态. 一.综述 useState:处理函数只改变引用的状态本身:副作用状态:会对引用状态以外的状态和变量进行修改:useReducer:用解藕化的机制 ...
- Good Article Good sentence HDU - 4416 (后缀数组)
Good Article Good sentence \[ Time Limit: 3000 ms\quad Memory Limit: 32768 kB \] 题意 给出一个 \(S\) 串,在给出 ...
- ansible 批量部署准备工作
Ansible:自动化运维工具,基于Python开发 功能{ 批量系统配置 批量程序部署 批量运行命令等等 } 准备工作: 一.操作主机安装epel源 和 ansible工具 yum -y insta ...
- js replace(a,b)替换指定字符
var a="aaabbb" a= a.replace("aaa", "ccc") console.log(a) //a ="c ...
- 封装好的observer.js,用于非父子组件传值,直接调用$on和$emit方法
const eventList = {} const $on = (eventName,callback)=>{ if(!eventList[eventName]){ eventList[eve ...