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

这个推了蛮久的。。。。。不太熟练

    //左子树
if(k-l2==){
tree[root].l = -;//左子树为空
}
else{
buildTree(*root,l1,l1+k-l2-,l2,k-);
}
//右子树
if(k+>r2){
tree[root].r = -;//右子树为空
}
else{
buildTree(*root+,l1+k-l2,r1-,k+,r2);
}

AC代码:

#include<bits/stdc++.h>
using namespace std;
int post[];
int in[];
int n;
struct node{
int v;
int l;
int r;
}tree[];
queue<int>q;
void buildTree(int root,int l1,int r1,int l2,int r2){//后序,中序
//cout<<root<<" "<<l1<<"-"<<r1<<" "<<l2<<"-"<<r2<<endl;
//先找根节点
tree[root].v = post[r1];
if(l1==r1){ //只有一个节点
tree[root].l = -;
tree[root].r = -;
return;
}else{
tree[root].l = *root;
tree[root].r = *root+;
}
//找一下根在中序上的位置
int k;
for(int i=l2;i<=r2;i++){
if(in[i]==post[r1]){
k=i;//前面k-l2个数就是左子树
break;
}
}
//左子树
if(k-l2==){
tree[root].l = -;//左子树为空
}
else{
buildTree(*root,l1,l1+k-l2-,l2,k-);
}
//右子树
if(k+>r2){
tree[root].r = -;//右子树为空
}
else{
buildTree(*root+,l1+k-l2,r1-,k+,r2);
}
}
int main(){
int n;
cin>>n;
for(int i=;i<=n;i++){
cin>>post[i];
}
for(int i=;i<=n;i++){
cin>>in[i];
}
buildTree(,,n,,n);
//bfs 层序
while(!q.empty()) q.pop();
q.push();
while(!q.empty()){
node x=tree[q.front()];
q.pop();
cout<<x.v;
if(x.l!=-){
q.push(x.l);
}
if(x.r!=-){
q.push(x.r);
}
if(!q.empty()){
cout<<" ";
}
}
return ;
}

PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)的更多相关文章

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

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

  2. PAT Advanced 1020 Tree Traversals (25 分)

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

  3. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  4. 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)

    题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...

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

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

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

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

  7. PAT 甲级 1020 Tree Traversals

    https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...

  8. 1020 Tree Traversals (25分)思路分析 + 满分代码

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  9. 1020 Tree Traversals (25 分)

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

随机推荐

  1. pynq系列__LED灯闪烁

    1.实现在board项中自动配置pynq-z2开发板 (1).board file下载地址:http://www.tul.com.tw/ProductsPYNQ-Z2.html   (2).下载之后得 ...

  2. webstorm如何调试vue项目的js

    webstorm如何调试vue项目的js webstormvuewebstorm调试jsjs 1.编辑调试配置,新建JavaScript调试配置,并设置要访问的url地址,如下图所示: 在URL处填写 ...

  3. [Python] Codecombat攻略 远边的森林 Forest (1-40关)

    首页:https://cn.codecombat.com/play语言:Python 第二界面:远边的森林Forest(40关)时间:2-6小时内容:if/else.关系操作符.对象属性.处理输入网页 ...

  4. 个人作业-Alpha项目测试—luomei1547

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/ 这个作业要求在哪里 https://edu.cnbl ...

  5. 单元测试框架之unittest(四)

    一.摘要 假设我们有一组测试方法差别非常小,比如仅仅是所需要的参数有少许变化时,我们的自动化测试如何进行?unittest框架为这种场景提供了一种方式,它允许我们用subTest()上下文管理器在一个 ...

  6. Vue入门到出门

    原来微信小程序的js跟这个差不多啊.这个也不像jQuery那种完全是为了方便写js的感觉,难道算前端框架?还不太了解,总之要先看看,然后用HBuilder快点上手做点东西…… ------------ ...

  7. .NET Core 3全新来袭!DevExpress Winforms v19.2支持High DPI

    DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅.美观且易于使用的应用程序.无论是Office风格的界面,还是分析处理大批量的业务数据,DevExpr ...

  8. Junit加载Spring容器作单元测试

    阅读目录 > 基本的搭建 > 常见的用法 如果我们需要对我们的Service方法作单元测试,恰好又是用Spring作为IOC容器的,我们可以这么配置Junit加载Spring容器,方便做单 ...

  9. 用strtotime()和date()函数算出2019年9月的周日日期

    strtotime---用于接收两个参数,第一个参数是格式化的日期数据如:date('Y-m-d'),第二个参数有如'+7 day' 函数版 <?php $firstsunday = strto ...

  10. 28、[源码]-AOP原理-@EnableAspectJAutoProxy

    28.[源码]-AOP原理-@EnableAspectJAutoProxy