当前位置:网站首页>Pat grade a A1020 tree Traversals
Pat grade a A1020 tree Traversals
2022-07-18 04:31:00 【Zhemu】
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
The question : Give how many nodes the binary tree has , The next first sequence is the post order traversal sequence , The second is the middle order ergodic sequence , According to these two sequences, find the hierarchical traversal sequence .
Ideas : Construct a binary tree according to two traversal sequences , Then use sequence traversal ( Breadth first search -- The branch always accesses the node that can be reached directly first ( Put the left and right children of the node into the queue first )) Find the hierarchical traversal sequence
Code :
#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;
};
// Construct binary tree according to sequence ;
node* create(int postL,int postR,int inL,int inR){
if(postL>postR)return NULL;// Recursive boundary ;
node* root=new node;// Construct the root node ;
root->data=post[postR];
int k;
for(k=inL;k<=inR;k++){// Notice that this is inL To inR, Find the root node in the subtree , Not from 0 To inR, And because the incoming is 0 To n-1, So it can be equal to inR Of ;
if(in[k]==root->data){
break;
}
}
int leftnum=k-inL;// Confirm the left and right subtrees ;
root->lchild=create(postL,postL+leftnum-1,inL,k-1);// Then enter the left subtree and repeat the operation , Bring two sequences into the function , Find the root node , Create a root node , Confirm the left and right subtrees , Until the subsequence is numbered ( When it is an empty tree ), return NULL;
root->rchild=create(postL+leftnum,postR-1,k+1,inR);// Recursive ;
return root;
}
// Breadth first search for hierarchical sequence ;
void BFS(node* root){
queue<node* > Q;
// Note that here is the pointer queue , Because the queue stores copies of binary tree nodes , If you want to modify the data of the binary tree , You need to use the pointer of this node , Go to the location of the node to modify , So this stores pointers ;
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;
}边栏推荐
猜你喜欢

开源数据质量解决方案——Apache Griffin入门宝典

JVM内存模型——运行时数据区的特点和作用

OpenAI发文介绍Dall·E 2最新应用情况:全面进入艺术创作和设计领域

tinymce5.0.8编辑器最新版本中文版

Technology sharing | sending requests using curl

JVM memory model -- Characteristics and functions of runtime data area

工业交换机的单模和多模能否互相替代?

Probe into parental delegation mechanism from source code
Gaussdb (DWS), the first benchmarking MySQL command collection article in the whole network

DPU — 完全可编程网络
随机推荐
盘点波卡生态潜力项目 | 跨链特性促进多赛道繁荣
【第二十四题】逻辑闭环(北理工/北京理工大学/程序设计方法与实践/小学期 )
Leetcode-31-next spread
js图片裁剪及压缩整合插件
Fade the background registration + login form page
Is it safe for qiniu business school to open an account? Is it reliable? How can qiniu open an account
波卡创始人 Gavin Wood:波卡治理 v2 会有哪些变化?
扁平化骑手注册表单
DPU - fully programmable network
语句和表达式有什么不同
使用定时器触发类型处理数据库数据,函数资源使用量中这个执行时间是怎么算的?
The relationship between reinforcement learning (Q-learning) and path search (a*)
JS effect - table interlaced color change
荷兰蒂尔堡大学、联邦大学 | Neural Data-to-Text Generation Based on Small Datasets: Comparing the Added Value of Two Semi-Supervised Learning Approaches on Top of a Large Language Model(基于小数据集的神经数据到文本生成)
太卷了, 某公司把自家运营多年的核心系统(智慧系统)完全开源了....
PAT 甲级 A1020 Tree Traversals
PAT 甲级 A1053 Path of Equal Weight(树的遍历)
二叉查找树的性质和用法
腾讯云EKS 上部署 eshopondapr
JVM memory model -- Characteristics and functions of runtime data area