当前位置:网站首页>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
边栏推荐
- 数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
- Flink customizes the application of sink side sinkfunction
- Build websocket server in. Net5 webapi
- QT dynamic translation of Chinese and English languages
- 批量下載文件----壓縮後再下載
- Generate QR code through zxing
- Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]
- 移植tslib时ts_setup: No such file or directory、ts_open: No such file or director
- C abstract class
- Advanced sorting - fast sorting
猜你喜欢

Supersocket is Use in net5 - startup

Configure automatic implementation of curd projects

2022 团体程序设计天梯赛 模拟赛 L2-1 盲盒包装流水线 (25 分)

QT learning summary

Xutils3 corrected a bug I reported. Happy

Super easy to use asynchronous export function of Excel

Unity knowledge points (ugui)

《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
![General testing technology [1] classification of testing](/img/f1/d80b6793b6443cbc4048d7e6319f51.png)
General testing technology [1] classification of testing

Flink customizes the application of sink side sinkfunction
随机推荐
浅学一下I/O流和File类文件操作
How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
"Visual programming" test paper
Unity basics 2
Top ten project management software similar to JIRA
Student achievement management
2022a special equipment related management (elevator) work license question bank and simulation examination
Flink customizes the application of sink side sinkfunction
Xutils3 corrected a bug I reported. Happy
[mock data] fastmock dynamically returns the mock content according to the incoming parameters
[Mysql] LEFT函數 | RIGHT函數
Knowledge of software testing~
Mysql database, inconsistent index character set, slow SQL query, interface timeout
MySql分组查询规则
可以接收多種數據類型參數——可變參數
C interface
Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
[MySQL] left Function | Right Function
12.<tag-链表和常考点综合>-lt.234-回文链表
AWS from entry to actual combat: creating accounts