当前位置:网站首页>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
边栏推荐
- SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
- Copy array in JS
- dataBinding中使用include
- 'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...
- xctf刷题小记
- STM32 uses Hal library. The overall structure and function principle are introduced
- Go语言自学系列 | golang方法
- jsp页面编码
- Word plus watermark
- 关于堆的判断 (25 分) 两种插入方式
猜你喜欢

bashdb下载安装

Reference passing 1

根据字节码获取类的绝对路径

Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting
![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]

Automatic differentiation and higher order derivative in deep learning framework

Let the earth have less "carbon" and rest on the road

Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library

idea底栏打开services

测试你的机器学习流水线
随机推荐
STM32 uses Hal library. The overall structure and function principle are introduced
JSP page coding
Trust uses Tokio's notify and timeout to achieve the effect similar to the timeout condition variable
在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
Use of applicationreadyevent
tsdf +mvs
MySQL查询两张表属性值非重复的数据
ONEFLOW learning notes: from functor to opexprinter
求简单类型的矩阵和
2022-04-22 OpenEBS云原生存储
Basic usage of synchronized locks
深度学习框架中的自动微分及高阶导数
Knowledge points and problem solutions related to information collection
L2-3 浪漫侧影 (25 分)
Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
Automatic differentiation and higher order derivative in deep learning framework
Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting
洋桃电子STM32物联网入门30步笔记一、HAL库和标准库的区别
Queue (C language / linked list)
DOM 学习之—添加+-按钮