当前位置:网站首页>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
边栏推荐
- idea打包 jar文件
- 引用传递1
- On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched
- uni-app和微信小程序中的getCurrentPages()
- 'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...
- LINQ Learning Series ----- 1.4 anonymous objects
- Redis Desktop Manager for Mac(Redis可视化工具)
- Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
- Record: JS several methods to delete one or more items in the array
- Go语言自学系列 | golang结构体指针
猜你喜欢
Consensus Token:web3.0生态流量的超级入口
2021李宏毅机器学习之Adaptive Learning Rate
After a circle, I sorted out this set of interview questions..
1099 建立二叉搜索树 (30 分)
On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched
Knowledge points and problem solutions related to information collection
'恶霸' Oracle 又放大招,各大企业连夜删除 JDK。。。
idea配置连接远程数据库MySQL,或者是Navicat连接远程数据库失败问题(已解决)
Test your machine learning pipeline
How to generate assembly file
随机推荐
还原二叉树 (25 分)
Add random attributes to the Li class array objects and sort them
Type anonyme (Principes fondamentaux du Guide c)
洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
什么是RPC
请问中衍期货安全靠谱吗?
1099 建立二叉搜索树 (30 分)
Idea is configured to connect to the remote database mysql, or Navicat fails to connect to the remote database (solved)
洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解
Idea import commons-logging-1.2 Jar package
Add listening event to input element
测试你的机器学习流水线
IDEA导入commons-logging-1.2.jar包
okcc呼叫中心外呼系统智能系统需要用多大的盘存录音?
Detailed description of self feeling of auricular point weight loss 0422
L2-3 romantic silhouette (25 points)
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
bashdb下载安装
Redis Desktop Manager for Mac(Redis可视化工具)
Go语言自学系列 | golang结构体的初始化