当前位置:网站首页>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
边栏推荐
- IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
- 2022a special equipment related management (elevator) work license question bank and simulation examination
- 2022年做跨境电商五大技巧小分享
- 为什么BI对企业这么重要?
- Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]
- Visual programming -- how to customize the mouse cursor
- 研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
- Iotos IOT middle platform is connected to the access control system of isecure center
- 场景题:A系统如何使用B系统的页面
- QT uses drag and drop picture to control and mouse to move picture
猜你喜欢

Peut recevoir plusieurs paramètres de type de données - paramètres variables

It can receive multiple data type parameters - variable parameters

Configure automatic implementation of curd projects

2022g2 boiler stoker examination question bank and online simulation examination

浅学一下I/O流和File类文件操作

C abstract class

Fiddler use

Is it difficult to choose binary version control tools? After reading this article, you will find the answer

MySQL keyword group_ Concat, combined connection query
![General testing technology [1] classification of testing](/img/f1/d80b6793b6443cbc4048d7e6319f51.png)
General testing technology [1] classification of testing
随机推荐
QT dynamic translation of Chinese and English languages
软件测试相关知识~
How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
Web Course Design - his system
MySQL query specifies that a row is sorted to the first row
JS, bind the event for a label with input, and then bind the stand-alone event in the parent element. The event is executed twice and solved
IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
2022 团体程序设计天梯赛 模拟赛 L1-7 矩阵列平移 (20 分)
可以接收多種數據類型參數——可變參數
JS recursive tree structure calculates the number of leaf nodes of each node and outputs it
Log4net is in Net core usage
IDEA查看历史记录【文件历史和项目历史】
2022g2 boiler stoker examination question bank and online simulation examination
Unity Basics
MySQL grouping query rules
Visual programming - drawing assignment
2022年做跨境电商五大技巧小分享
Charles uses three ways to modify requests and responses
Unity basics 2