PAT Advanced 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 (<=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
- int in[n]记录中序序列,int post[n]记录后序序列
- 建树
2.1 post[n-1]为当前根结点root,在in中序序列中找到当前根结点root的位置i,i左边的元素为root的左子树所有结点,i右边元素为root的右子树所有结点
2.2 将root左子树所有结点作为后序序列继续查找其root和左右子树节点,将root右子树所有结点作为后序序列继续查找其root和左右子树结点 - 层序遍历树
思路 02(最优)
- int in[n]记录中序序列,int post[n]记录后序序列,node结构体中增加index属性记录树使用数组存储时的节点下标
- 前序遍历(利用中序和后序序列,依次找出根节点和其左右子树节点),设置其index,若某根节点index=i,则其左子节点index=2*i+1,其右子节点index=2*i+2
- 排序:根据index升序对所有结点排序
- 打印排序后的所有结点
思路 03
思路02中的index换为记录层号,对层号进行排序
注:不能使用sort,因为sort不稳定,导致同层顺序得不到保证,需要使用stable_sort()
知识点
- 已知中序序列和前序、后序、层序中任意一个序列,可唯一确定一棵二叉树
- 后序序列最后一个节点为根节点,该根节点将中序序列一分为二,左边为根节点左子树,右边为根节点右子树
- 树的层序遍历(借助队列)
- 根据后序序列和中序序列建树
- 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) [⼆叉树的遍历,后序中序转层序]的更多相关文章
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT Advanced 1138 Postorder Traversal (25) [树的遍历,前序中序转后序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and ...
- 【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 (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 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)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 1020. Tree Traversals (25) ——树的遍历
//题目 通过后续遍历 中序遍历 得出一棵树 ,然后按树的层次遍历打印 PS:以前对于这种用指针的题目是比较头痛的,现在做了一些链表操作后,感觉也不难 先通过后续中序建一棵树,然后通过BFS遍历这棵树 ...
- 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 ...
随机推荐
- Bug 佛祖镇楼
原文链接:https://www.cnblogs.com/xdp-gacl/p/4198935.html // _ooOoo_ // o8888888o // 88" . "88 ...
- 027-PHP编码和解码函数base64
<?php $data = "我爱PHP";//解码前的值 print("我爱PHP: " . base64_encode($data)); //进行解码 ...
- 080-PHP的if与elseif用法
<?php /* 正确的使用方法: */ $a = 10; $b = 20; if ($a > $b): echo $a . "大于" . $b; elseif ($a ...
- (22)Canny算法
基础知识,主要是看这个博客:https://blog.csdn.net/qq_41167777/article/details/84863351
- SpringBoot+SpringSecurity之如何forword到登录页面
当我们在项目中引入了SpringSecurity框架进行身份校验的时候,如果某个请求需要用户身份认证,那么SpringSecurity会将用户redirect到登录页面.但是有些时候我们希望是forw ...
- python语法技巧
目录: 7.python break continue用法 1.三元表达式 2.用列表推导式来取代map和filter 3.函数式编程 4.fluent python 阅读笔记 5.enum库用法 6 ...
- iOS 保存图片(视频)到相册
1.C语言函数方式实现 注意:UIImageWriteToSavedPhotosAlbum方法必须实现代理方法,否则会崩溃. //参数1:图片对象 //参数2:成功方法绑定的target //参数3: ...
- Caused by: com.mysql.cj.exceptions.DataReadException: Zero date value prohibited
原因:数据库日期出现零值,即0000-00-00 属于一个无效日期. 解决方案:重新赋值,或者在jdbc链接后加参数zeroDateTimeBehavior=convertToNull
- Integer和int的区别
1.Integer是int的包装类,int则是java的一种基本数据类型 2.Integer变量必须实例化后才能使用,而int变量不需要 3.Integer实际是对象的引用,当new一个Integer ...
- POJ 1469:COURSES
COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19458 Accepted: 7658 Descript ...