当前位置:网站首页>Romantic silhouette of L2-3 of 2022 group programming ladder Simulation Competition (25 points)
Romantic silhouette of L2-3 of 2022 group programming ladder Simulation Competition (25 points)
2022-04-23 03:23:00 【One more line and go to bed】
“ 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
It's a test of basic skills
#include <bits/stdc++.h>
using namespace std;
struct t{
t *l,*r;
int x,f;
};
void op(int pl,int pr,int ql,int qr,int *p,int *q,t* x){
int c=q[qr],ps=pl; // Find the value of the root node in the subsequent traversal
while(p[ps]!=c)ps++; // Find the value of the root node in the middle order traversal
x->x=c; // Assign a value to the root node
if(ps!=pl){ // If I have a left subtree
t* sl=(t*)malloc(sizeof(t)); // The new node
sl->l=NULL; // The left child node is set to null
sl->r=NULL; // The right child node is set to null
sl->f=x->f+1; // Record the number of layers of the new node
x->l=sl; // Connect the new node to the root node
op(pl,ps-1,ql,ql+ps-pl-1,p,q,sl); // Recursive operation
}
if(ps!=pr){ // If I have a right subtree , The process is similar to
t* sr=(t*)malloc(sizeof(t));
sr->l=NULL;
sr->r=NULL;
sr->f=x->f+1;
x->r=sr;
op(ps+1,pr,ql+ps-pl,qr-1,p,q,sr);
}
}
int main(void){
int n,*p,*q;
t* s=(t*)malloc(sizeof(t)); // The root node
s->l=NULL; // The left child node of the root node is set to null
s->r=NULL; // The right child node of the root node is set to null
s->f=0; // The root node hierarchy is set to 0
cin>>n;
p=(int*)malloc(sizeof(int)*n); // Middle order array
q=(int*)malloc(sizeof(int)*n); // Post order array
for(int i=0;i<n;i++)cin>>p[i];
for(int i=0;i<n;i++)cin>>q[i];
op(0,n-1,0,n-1,p,q,s); // Build up trees
vector<t*> v; // Sequence access
v.push_back(s);
for(int i=0;i<n;i++){
if(v[i]->l)v.push_back(v[i]->l);
if(v[i]->r)v.push_back(v[i]->r);
}
stack<int> rp; // Right side view , Traversal in reverse order , Aided stack
int rv[n]={0}; // The right view accesses the array , Record accessed hierarchy
for(int i=v.size()-1;i>=0;i--){
if(!rv[v[i]->f]){
rv[v[i]->f]=1;
rp.push(v[i]->x);
}
}
cout<<"R:";
while(!rp.empty()){
cout<<' '<<rp.top();
rp.pop();
}
cout<<endl<<"L:";
int lv[n]={0}; // The left view accesses the array , Direct output
for(int i=0;i<v.size();i++){
if(!lv[v[i]->f]){
lv[v[i]->f]=1;
cout<<' '<<v[i]->x;
}
}
return 0;
}
版权声明
本文为[One more line and go to bed]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230322232802.html
边栏推荐
- 超好用的【通用Excel导入功能】
- Detailed explanation of socket programming send() and recv() functions
- Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
- Huawei mobile ADB devices connection device is empty
- Mysql database
- It can receive multiple data type parameters - variable parameters
- 2022 团体程序设计天梯赛 模拟赛 L2-1 盲盒包装流水线 (25 分)
- Supersocket is Use in net5 - concept
- 通过 zxing 生成二维码
- Batch download of files ---- compressed and then downloaded
猜你喜欢
[mock data] fastmock dynamically returns the mock content according to the incoming parameters
[untitled]
Node configuration environment CMD does not take effect
Huawei mobile ADB devices connection device is empty
Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
Unity basics 2
MySQL之explain关键字详解
2022 团体程序设计天梯赛 模拟赛 L2-1 盲盒包装流水线 (25 分)
Web Course Design - his system
Course design of Database Principle -- material distribution management system
随机推荐
全新的ORM框架——BeetlSQL介绍
可以接收多種數據類型參數——可變參數
2022 团体程序设计天梯赛 模拟赛 L2-1 盲盒包装流水线 (25 分)
Problem a: face recognition
Fiddler use
js 中,为一个里面带有input 的label 绑定事件后在父元素绑定单机事件,事件执行两次,求解
数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
Eight elder brothers chronicle [4]
Node configuration environment CMD does not take effect
Supersocket is Use in net5 - startup
There is no index in the database table. When inserting data, SQL statements are used to prevent repeated addition (Reprint)
Mysql database, inconsistent index character set, slow SQL query, interface timeout
Quartz. Www. 18fu Used in net core
Téléchargement en vrac de fichiers - téléchargement après compression
集合之List接口
IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
Supersocket is Use in net5 - concept
General test technology [II] test method
Flink real-time data warehouse project - Design and implementation of DWS layer
Seminar playback video: how to improve Jenkins' ability to become a real Devops platform