当前位置:网站首页>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
边栏推荐
- ONEFLOW learning notes: from functor to opexprinter
- Consensus Token:web3.0生态流量的超级入口
- OneFlow學習筆記:從Functor到OpExprInterpreter
- 【58】最后一个单词的长度【LeetCode】
- Search tree judgment (25 points)
- Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
- DJ音乐管理软件Pioneer DJ rekordbox
- RPC procedure
- RCC introduction of Hal Library
- 2022-04-22 OpenEBS云原生存储
猜你喜欢

洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別

JVM工具之Arthas使用
![Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]](/img/67/1f9df4236b0aac3480836d45ab8561.png)
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]

Use of Arthas in JVM tools

idea底栏打开services

Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting

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

STM32使用HAL库,整体结构和函数原理介绍

HAL库的RCC简介

Transformer XL: attention language modelsbbeyond a fixed length context paper summary
随机推荐
Reference passing 1
Misunderstanding of flush () method of OutputStream class
okcc呼叫中心外呼系统智能系统需要用多大的盘存录音?
rembg 分割mask
L2-3 romantic silhouette (25 points)
关于堆的判断 (25 分) 两种插入方式
Navicat远程连接mysql
DOM学习笔记---遍历页面所有元素节点
面了一圈,整理了这套面试题。。
STM32 uses Hal library. The overall structure and function principle are introduced
深度学习框架中的自动微分及高阶导数
ONEFLOW learning notes: from functor to opexprinter
L2-3 浪漫侧影 (25 分)
队列(c语言/链表)
Stm32f103zet6 [development of standard library functions] - Introduction to library functions
Notes on 30 steps of introduction to the Internet of things of yangtao electronics STM32 III. cubemx graphical programming and setting the IO port on the development board
Ear acupoint diagnosis and treatment essay 0421
Queue (C language / linked list)
Large amount of data submitted by form post
STM32使用HAL库,整体结构和函数原理介绍