题目

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 (<=30), 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

题目分析

已知后序序列和中序序列,打印层序序列

解题思路

思路 01

  1. int in[n]记录中序序列,int post[n]记录后序序列
  2. 建树

    2.1 post[n-1]为当前根结点root,在in中序序列中找到当前根结点root的位置i,i左边的元素为root的左子树所有结点,i右边元素为root的右子树所有结点

    2.2 将root左子树所有结点作为后序序列继续查找其root和左右子树节点,将root右子树所有结点作为后序序列继续查找其root和左右子树结点
  3. 层序遍历树

思路 02(最优)

  1. int in[n]记录中序序列,int post[n]记录后序序列,node结构体中增加index属性记录树使用数组存储时的节点下标
  2. 前序遍历(利用中序和后序序列,依次找出根节点和其左右子树节点),设置其index,若某根节点index=i,则其左子节点index=2*i+1,其右子节点index=2*i+2
  3. 排序:根据index升序对所有结点排序
  4. 打印排序后的所有结点

思路 03

思路02中的index换为记录层号,对层号进行排序

注:不能使用sort,因为sort不稳定,导致同层顺序得不到保证,需要使用stable_sort()

知识点

  1. 已知中序序列和前序、后序、层序中任意一个序列,可唯一确定一棵二叉树
  2. 后序序列最后一个节点为根节点,该根节点将中序序列一分为二,左边为根节点左子树,右边为根节点右子树
  3. 树的层序遍历(借助队列)
  4. 根据后序序列和中序序列建树
  5. bfs层级遍历时,可以使用数组存储树的特点(即节点存储在下标i,则其左子节点下标为2i+1,右子节点下标为2i+2),之后对下标进行升序排序,遍历即为层序序列;

Code

Code 01

#include <iostream>
#include <queue>
using namespace std;
const int maxn=30;
int pre[maxn],in[maxn],post[maxn];
int n; // the number of nodes
struct node {
int data;
node* left;
node* right;
};
/*
后序序列和中序序列建树
*/
node * create(int postL,int postR,int inL,int inR) {
if(postL>postR)return NULL;
node * root=new node;
root->data=post[postR];
// 找到当前根节点在中序遍历中的位置
int i;
for(i=inL; i<=inR; i++) {
if(post[postR]==in[i])
break;
}
int numLeft=i-inL;
root->left=create(postL,postL+numLeft-1,inL,inL+numLeft-1);//inL+numLeft=i
root->right=create(postL+numLeft,postR-1,inL+numLeft+1,inR);//inL+numLeft=i
return root;
}
/*
树层序遍历
*/
int num=0;
void BFS(node * root) {
queue<node*> q;
q.push(root);
while(!q.empty()) {
node * now = q.front();
q.pop();
printf("%d",now->data);
num++;
if(num<n)printf(" ");
if(now->left!=NULL)q.push(now->left);
if(now->right!=NULL)q.push(now->right);
}
}
int main(int argc,char * argv[]) {
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&post[i]);
for(int i=0; i<n; i++)
scanf("%d",&in[i]);
node* root=create(0,n-1,0,n-1);
BFS(root);
return 0;
}

Code 02(最优)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=30;
int n,in[maxn],post[maxn];
struct node {
int data;
int index;
};
vector<node> vns;
bool cmp(node &n1,node &n2){
return n1.index<n2.index;
}
void pre(int root,int start,int end,int index) {
if(start>end)return;
vns.push_back({post[root],index});
int i;
while(i<end&&in[i]!=post[root])i++;
pre(root-(end-i)-1,start,i-1,2*index+1);
pre(root-1,i+1,end,2*index+2);
}
int main(int argc,char *argv[]) {
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&post[i]);
for(int i=0; i<n; i++)
scanf("%d",&in[i]);
pre(n-1,0,n-1,0);
sort(vns.begin(),vns.end(),cmp);
for(int i=0;i<vns.size();i++){
if(i!=0)printf(" ");
printf("%d",vns[i].data);
}
return 0;
}

Code 03

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=30;
int n,in[maxn],post[maxn];
struct node{
int d;
int l;
};
vector<node> pre;
void t_pre(int pl,int pr,int il,int ir,int level){
if(il>ir)return;
pre.push_back({post[pr],level});
int k=il;
while(k<n&&in[k]!=post[pr])k++;
t_pre(pl,pl+k-il-1,il,k-1,level+1);
t_pre(pl+k-il,pr-1,k+1,ir,level+1);
}
bool cmp(const node &a,const node &b){
return a.l<b.l;
}
int main(int argc,char * argv[]) {
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&post[i]);
for(int i=0; i<n; i++)
scanf("%d",&in[i]);
t_pre(0,n-1,0,n-1,1);
stable_sort(pre.begin(),pre.end(),cmp);
printf("%d",pre[0]);
for(int i=1;i<n;i++)
printf(" %d",pre[i].d);
return 0;
}

PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]的更多相关文章

  1. PAT Advanced 1020 Tree Traversals (25 分)

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

  2. PAT Advanced 1138 Postorder Traversal (25) [树的遍历,前序中序转后序]

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

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

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

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

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

  5. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

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

  6. 【PAT】1020. Tree Traversals (25)

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

  7. 1020. Tree Traversals (25) ——树的遍历

    //题目 通过后续遍历 中序遍历 得出一棵树 ,然后按树的层次遍历打印 PS:以前对于这种用指针的题目是比较头痛的,现在做了一些链表操作后,感觉也不难 先通过后续中序建一棵树,然后通过BFS遍历这棵树 ...

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

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

  9. PAT 甲级 1020 Tree Traversals

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

随机推荐

  1. 十三、JavaScript之跨多行的变量申明

    一.代码如下 二.运行效果 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" conten ...

  2. 用cmd运行java可以javac不行(win10)

    今天发现个有趣的问题,用cmd运行java可以javac不行.(win10) java-home和classpath配置没有问题,最后发现问提出先在path,在这里看并没有异常. 在上面图片中点击编辑 ...

  3. P4147 玉蟾宫(悬线法求最大子矩阵)

    P4147 玉蟾宫 悬线法 ,\(l_{i,j},r_{i,j},up_{i,j}\) 分别表示 \((i,j)\) 这个点向左,右,上能到达的远点.然后面积就很好办了.具体实现见代码. 然而,还有更 ...

  4. 关于低成本Sub-1Ghz无线射频方案首选:CMT2110A

    对于现在的无线遥控市场竞争力极大,不过是在成本方面和性能方面大家都希望能做到精打细算,目前超低功耗Sub-1Ghz 首选方案是CMT2110/17A 芯片具有超低成本,高性能灵活的单芯片OOK发射器, ...

  5. C++编程学习(一) 概述

    从一年前开通博客,陆陆续续写了一些总结类的东西,但是一直没push到网上.现在发现,分享自己的笔记也是自身学习.与他人交流的好方式.从今天开始,我会经常的push一些学习笔记到博客中,先从C++开始吧 ...

  6. k8s资源pod yaml文件分析

    apiVersion: v1 kind: Pod_name metadata name: pod_name #描述Pod的名字 namespace: default #描述Pod所在命名空间,如果不设 ...

  7. JSTL 运算符汇总

    算术运算符 + . - . * . / (或 div )和 % (或 mod )  关系运算符 == (或 eq ). != (或 ne ). < (或 lt ). > (或 gt ). ...

  8. C#高级编程(第9版) 第06章 数组

    好久没发东西了 , 一停下来就会变懒.... 虽然没完成,也就是它吧 --------------------------------- 以下正文 -------------------------- ...

  9. Invalid bean definition with name 'dataSource' defined in class path resource [applicationContext.xml]

    启动tomcat,访问一个web项目失败,查看日志,发现异常信息: 18-Jul-2019 15:22:16.822 严重 [main] org.apache.catalina.core.Standa ...

  10. POJ 1164:The Castle

    The Castle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6677   Accepted: 3767 Descri ...