当前位置:网站首页>L2-3 romantic silhouette (25 points)
L2-3 romantic silhouette (25 points)
2022-04-23 08:41:00 【Huaihua second affectionate】

“ Profile ” Is to observe what the object sees from the left or right . For example, the silhouette of the boy in the above picture is what he looks at from his right side , It's called “ Right side view ”; The girl's silhouette looks from her left , It's called “ Left view ”.
520 You are still playing this day , Just hold a binary tree and look left and right ……
We will the of binary tree “ Profile ” Defined as a sequence of all nodes visible from one side from top to bottom . For example, the binary tree in the figure below , The right view is { 1, 2, 3, 4, 5 }, The left view is { 1, 6, 7, 8, 5 }.

So let's first build a tree through the middle order traversal sequence and post order traversal sequence of a binary tree , Then you have to output the left and right views of the tree .
Input format :
The first line of input gives a positive integer N (≤20), Is the number of nodes in the tree . Then, the middle order traversal and post order traversal sequences of the tree are given in two lines . All key values in the tree are different , Its value is irrelevant , Not more than int The scope of the .
Output format :
The first line outputs the right view , The second line outputs the left view , The format is shown in the example .
sample input :
8
6 8 7 4 5 1 3 2
8 5 4 7 6 3 2 1
sample output :
R: 1 2 3 4 5
L: 1 6 7 8 5
#include<bits/stdc++.h>
#define ll long long
#define speed_up ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
struct Node{
int data;
Node* left;
Node* right;
};
typedef Node* Tree;
int n;
int in[40],post[40];
map<int,int>pos;
vector<int>L,R;
Tree build(int il,int ir,int pl,int pr){
Tree root=new(Node);
root->left=NULL;
root->right=NULL;
root->data=post[pr];
int k=pos[post[pr]];
int size=k-il;
if(il<k){
root->left=build(il,k-1,pl,pl+size-1);
}
if(ir>k){
root->right=build(k+1,ir,pl+size,pr-1);
}
return root;
}
void bfs(Tree root){
queue<Tree>p;
p.push(root);
while(p.size()){
int n=p.size();
for(int i=0;i<n;i++){// The only detail , Find the leftmost and rightmost
Tree t=p.front();
p.pop();
if(i==0)L.push_back(t->data);
if(i==n-1)R.push_back(t->data);
if(t->left)p.push(t->left);
if(t->right)p.push(t->right);
}
}
}
int main(){
speed_up
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];
}
Tree B=build(0,n-1,0,n-1);
bfs(B);
cout<<"R:";
for(int i=0;i<R.size();i++)
cout<<" "<<R[i];
cout<<"\nL:";
for(int i=0;i<L.size();i++)
cout<<" "<<L[i];
return 0;
}
版权声明
本文为[Huaihua second affectionate]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230835222807.html
边栏推荐
猜你喜欢

00后最关注的职业:公务员排第二,第一是?

cadence的工艺角仿真、蒙特卡洛仿真、PSRR

Get the absolute path of the class according to the bytecode

Shell脚本进阶

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

洋桃电子STM32物联网入门30步笔记四、工程编译和下载

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

Harbor企业级镜像管理系统实战

【58】最后一个单词的长度【LeetCode】

Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
随机推荐
Go语言自学系列 | golang结构体指针
STM32使用HAL库,整体结构和函数原理介绍
对OutputStream类的flush()方法的误解
JS中复制数组
Go语言自学系列 | golang方法
Idea import commons-logging-1.2 Jar package
cadence的工艺角仿真、蒙特卡洛仿真、PSRR
Get the absolute path of the class according to the bytecode
Stm32f103zet6 [development of standard library functions] - Introduction to library functions
记录:js删除数组中某一项或几项的几种方法
关于cin,scanf和getline,getchar,cin.getline的混合使用
Asan minimalism
PDF with watermark
How much inventory recording does the intelligent system of external call system of okcc call center need?
Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
Navicat远程连接mysql
Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting
'恶霸' Oracle 又放大招,各大企业连夜删除 JDK。。。
HAL库的RCC简介
LaTeX数学公式