当前位置:网站首页>Play with binary tree (25 points)
Play with binary tree (25 points)
2022-04-23 08:41:00 【Huaihua second affectionate】
Given a binary tree in order to traverse and preorder traversal , Please mirror the tree first , And then output the inverted sequence traversal sequence . The so-called mirror reversal , It means to exchange the left and right children of all non leaf nodes . It is assumed that the key values are all positive integers which are not equal to each other .
Input format :
The first line of input gives a positive integer N(≤30), Is the number of nodes in a binary tree . The second line gives the middle order traversal sequence . The third line gives its preorder traversal sequence . Numbers are separated by spaces .
Output format :
Output the sequence traversed by the inverted sequence of the tree in one line . Between the numbers 1 Space separation , There must be no extra space at the beginning and end of the line .
sample input :
7
1 2 3 4 5 6 7
4 1 3 2 6 5 7
sample output :
4 6 1 7 5 3 2
#include<bits/stdc++.h>
using namespace std;
const int N=40;
map<int,int>l,r,pos;
int post[N],in[N];
int n,cnt;
int build(int il,int ir,int pl,int pr){
int root=post[pl];
int k=pos[root];
if(il<k)l[root]=build(il,k-1,pl+1,pl+k-il);
if(ir>k)r[root]=build(k+1,ir,pl+k-il+1,pr);
return root;
}
void bfs(int root) //BFS Used to traverse the output
{
queue<int> q;
q.push(root);
while (q.size())
{
auto t = q.front();
q.pop();
if(cnt==0)cout<<t;
else cout <<" "<<t;
cnt++;
if (r.count(t)) q.push(r[t]); // If it exists, join the queue , Wait for the next layer to traverse
if (l.count(t)) q.push(l[t]); // Judge whether the left and right sons of the node exist
}
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>in[i];
pos[in[i]]=i;
}
for(int i=0;i<n;i++)cin>>post[i];
int root=build(0,n-1,0,n-1);
bfs(root);
return 0;
}
版权声明
本文为[Huaihua second affectionate]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230835222500.html
边栏推荐
- bashdb下载安装
- ONEFLOW learning notes: from functor to opexprinter
- 洋桃电子STM32物联网入门30步笔记四、工程编译和下载
- 让地球少些“碳”息 度能在路上
- rembg 分割mask
- Word plus watermark
- Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
- MATLAB 画五星红旗
- 根据字节码获取类的绝对路径
- Misunderstanding of flush () method of OutputStream class
猜你喜欢

第一性原理 思维导图

Test your machine learning pipeline

Idea import commons-logging-1.2 Jar package

STM32 uses Hal library. The overall structure and function principle are introduced

MySQL查询两张表属性值非重复的数据

L2-3 romantic silhouette (25 points)

OneFlow学习笔记:从Functor到OpExprInterpreter

Transformer XL: attention language modelsbbeyond a fixed length context paper summary

RCC introduction of Hal Library

'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...
随机推荐
Detailed description of self feeling of auricular point weight loss 0422
What is RPC
增强现实技术是什么?能用在哪些地方?
Ear acupoint diagnosis and treatment essay 0421
RPC过程
'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...
Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing
How to generate assembly file
1099 建立二叉搜索树 (30 分)
RPC procedure
L2-3 romantic silhouette (25 points)
应纳税所得额
GUI编程简介 swing
Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动
ESP32程序下载失败,提示超时
搜索树判断 (25 分)
Complete binary search tree (30 points)
swagger文档导出自定义v2/api-docs拦截
让地球少些“碳”息 度能在路上
JSP page coding