当前位置:网站首页>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
边栏推荐
猜你喜欢

Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动

Idea is configured to connect to the remote database mysql, or Navicat fails to connect to the remote database (solved)

增强现实技术是什么?能用在哪些地方?
![[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel](/img/7c/0adc0940b6d5c8a61d34bfa5f66ee7.png)
[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel

在sqli-liabs学习SQL注入之旅(第十一关~第二十关)

IDEA导入commons-logging-1.2.jar包

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

洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口

xctf刷题小记

HAL库的RCC简介
随机推荐
在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
Complete binary search tree (30 points)
SYS_ CONNECT_ BY_ Path (column, 'char') combined with start with connect by prior
记录:js删除数组中某一项或几项的几种方法
00后最关注的职业:公务员排第二,第一是?
让地球少些“碳”息 度能在路上
请问中衍期货安全靠谱吗?
Word plus watermark
RPC过程
synchronized 锁的基本用法
STM32F103ZET6【标准库函数开发】----库函数介绍
Asan minimalism
Search tree judgment (25 points)
应纳税所得额
Queue (C language / linked list)
OneFlow学习笔记:从Functor到OpExprInterpreter
Excle plus watermark
Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing
测试你的机器学习流水线
Harbor企业级镜像管理系统实战