题目

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. 【CF1154G】Minimum Possible LCM

    题意 给你 \(n\) 个数 \(a_i\) ,求出 \(\text{lcm}\) 最小的一对数. \(n\le 10^6, a_i\le 10^7\) 题解 直接枚举 ,找到当前数最小的两个倍数,统 ...

  2. spring教程

    Spring框架是Java EE开发中最流行的框架,已经成为JEE事实上的标准,全世界的开发人员都在使用Spring框架开发各种应用.随着Spring boot,Spring cloud新版本的不断推 ...

  3. 关于c++静态类的说法

    看了网上的观点,大致有这几种比较好的. 观点一:根据现代的C++观点,静态类没有必要存在于C++中. 一个类是对一个概念的描述,类的本质是它维护了一个不变式,也就是说它有一个状态,它所有的接口都是为了 ...

  4. Origin中使用CopyPage复制图片到Word后比例失调解决办法

    Origin画图的优势很多,其图形美观易于操作.对我而言,Origin最大的优点就是与Word兼容,在Origin操作界面空白处直接使用右键CopyPage命令,然后在Word中使用粘贴命令即可插入图 ...

  5. 批量go get 代码

    批量 go get 代码 Go

  6. Stuts2与SpringMVC

    Struts2:一个基于MVC设计模式的Web应用框架,本质上相当于一个servlet.以WebWork为核心,采用拦截器的机制处理用户的请求(Filter). 轻量级的MVC框架.低侵入性,与业务代 ...

  7. SQL的7种连接查询详细实例讲解

    SQL的7种连接查询详细实例讲解 原文链接:https://mp.weixin.qq.com/s/LZ6BoDhorW4cSBhaGy8VUQ 在使用数据库查询语句时,单表的查询有时候不能满足项目的业 ...

  8. DNS bind9安装

    参考   111 首先要成功安装Centos操作系统,最新版本是Centos 6.4版本,最小化安装. [root@localhost named]# ifconfig -a     0 1 eth1 ...

  9. date linux系统校正时间

    date命令使用 -d<字符串>  显示字符串所指的日期与时间.字符串前后必须加上双引号.   date -d '13 second ago' ‘+%T’  13秒前   date +%T ...

  10. PHP中Cookie与Session的异同以及使用

    Cookie与Session的异同: 一.cookie机制 Cookies是服务器在本地机器上存储的小段文本并随每一个请求发送至同一个服务器.IETF RFC 2965 HTTP State Mana ...