当前位置:网站首页>PAT 甲级 A1020 Tree Traversals
PAT 甲级 A1020 Tree Traversals
2022-07-15 14:02:00 【柘木木】
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
题意:给出该二叉树有多少个结点,后面的第一个序列是后序遍历序列,第二个是中序遍历序列,根据这两个序列求层次遍历序列。
思路:根据两个遍历序列构造二叉树,然后用层序遍历(广度优先搜索--分岔口总是先访问可以直接到达的结点(将结点的左右孩子先放入队列内))求出层次遍历序列
代码:
#include<iostream>
#include<queue>
using namespace std;
const int maxn=50;
int in[maxn],post[maxn];
int n;
struct node{
int data;
node* lchild;
node* rchild;
};
//根据序列构造二叉树;
node* create(int postL,int postR,int inL,int inR){
if(postL>postR)return NULL;//递归边界;
node* root=new node;//构造根节点;
root->data=post[postR];
int k;
for(k=inL;k<=inR;k++){//注意这里是inL到inR,在子树内寻找跟根节点,不是从0到inR,还有因为传入的是0到n-1,所以是可以等于inR的;
if(in[k]==root->data){
break;
}
}
int leftnum=k-inL;//确认左右子树;
root->lchild=create(postL,postL+leftnum-1,inL,k-1);//然后进入左子树重复该操作,将两个序列带入函数,找到根节点,创建根节点,确认左右子树,直到子序列没数时(为空树的时候),返回NULL;
root->rchild=create(postL+leftnum,postR-1,k+1,inR);//递归式;
return root;
}
//广度优先搜索求层次序列;
void BFS(node* root){
queue<node* > Q;
//注意这里是指针队列,因为队列内存放的是二叉树结点的副本,如果想要对二叉树的数据进行修改,需要用到该结点的指针,去到该结点的位置进行修改,因此这存放的是指针;
Q.push(root);
int num=0;
while(!Q.empty()){
node* top=Q.front();
Q.pop();
printf("%d",top->data);
num++;
if(num<n)printf(" ");
if(top->lchild!=NULL)Q.push(top->lchild);
if(top->rchild!=NULL)Q.push(top->rchild);
}
}
int main( ){
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;
}边栏推荐
- 工业以太网交换机的产品性能有哪些呢?
- Flat rider registration form
- mysql数据库迁移到kingbase数据库上(其他数据库与其类似)
- Fade the background registration + login form page
- gradle 打包排除依赖 排除文件
- Write it down once Net analysis of memory leakage in web system of a power plant
- 【数值分析练习】三阶矩阵jacobi迭代法
- ThreadLocal原理及源码解析(一步一步点进去,不要背了,学思想)
- 在 Polkadot 中进行创建的三种方式 —— 平行链、平行线程、智能合约
- DAY_4作业——判断数组内是否有某一个数据————实现数组映射(放大 10 倍)—— 实现按序插入数组(修改bug)——实现数组去重
猜你喜欢
随机推荐
Responsive user login form
工业以太网交换机的产品性能有哪些呢?
Good news | the scores of candidates in this area can be queried
leetcode 605. Can place flowers planting problem (simple)
Is it safe for qiniu business school to open an account? Is it reliable? How can qiniu open an account
40 + times improvement, explain in detail how to optimize the performance of juicefs metadata backup and recovery
强化学习(Q-Learning)与路径搜索(A*)的联系
Processes in Oracle
P4 可编程网卡 — In-Network Computing(在网计算)
Interprocess communication - shared memory
JVM垃圾收集—垃圾收集器及常见组合参数
语句和表达式有什么不同
【走进go的内心深处】
JS image editor plug-in filerobot
BLOOM模型背后的技术实践:1760亿参数模型如何炼成?
Intel IPU
把一个数组封装成类
Class的生命周期
Fungible DPU
波卡创始人 Gavin Wood:波卡治理 v2 会有哪些变化?









