题目

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. 负载均衡与CDN简介

    负载均衡 负载均衡是高可用网络基础架构的的一个关键组成部分,有了负载均衡,我们通常可以将我们的应用服务器部署多台,然后通过负载均衡将用户的请求分发到不同的服务器用来提高网站.应用.数据库或其他服务的性 ...

  3. Flink 历史服务与连接器

    History Server(历史服务) Flink提供了记录历史任务运行情况的服务,可用于在关闭Flink群集后依然能够查询已完成作业的相关信息. 配置: # 任务执行信息存储在hdfs目录 job ...

  4. 逆向-PE导出表

    PE-导出表 ​ 动态链接库要想给别人用实现加载时或运行时链接就必须提供函数和数据的地址.exe一般不会有这个,大部分是DLL文件的.导出分为名字导出和序号导出. 名字导出先找名字,再通过名字表的索引 ...

  5. P 1033 旧键盘打字

    转跳点:

  6. #define、#undef、#ifdef、#ifndef、#if、#elif、#else、#endif、defined解释

    #define.#undef.#ifdef.#ifndef.#if.#elif.#else.#endif.defined. #define            定义一个预处理宏#undef     ...

  7. 洛谷 P1470 最长前缀 Longest Prefix

    题目传送门 解题思路: 其实思路没那么难,就是题面不好理解,解释一下题面吧. 就是在下面的字符串中找一个子串,使其以某种方式被分解后,每部分都是上面所给集合中的元素. AC代码: #include&l ...

  8. JAVAEE 和项目开发(第五课:服务器软件的介绍)

    Web服务器根据对javaEE支持的能力分为两大类 1,JavaEE服务器(应用服务器) 1) IBM公司 WebSphere 2) BEA公司 WebLogic 3) JBoss 公司 JBoss ...

  9. No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing inst

    今日遇到一个报错如下: No enclosing instance of type test is accessible. Must qualify the allocation with an en ...

  10. Ubuntu16.04安装配置Caffe教程(GPU版)

    推荐博客:https://www.linuxidc.com/Linux/2017-11/148629.htmhttps://blog.csdn.net/yggaoeecs/article/detail ...