当前位置:网站首页>L2-3 浪漫侧影 (25 分)
L2-3 浪漫侧影 (25 分)
2022-04-23 08:35:00 【怀化第二深情】
“侧影”就是从左侧或者右侧去观察物体所看到的内容。例如上图中男生的侧影是从他右侧看过去的样子,叫“右视图”;女生的侧影是从她左侧看过去的样子,叫“左视图”。
520 这个日子还在打比赛的你,也就抱着一棵二叉树左看看右看看了……
我们将二叉树的“侧影”定义为从一侧能看到的所有结点从上到下形成的序列。例如下图这棵二叉树,其右视图就是 { 1, 2, 3, 4, 5 },左视图就是 { 1, 6, 7, 8, 5 }。
于是让我们首先通过一棵二叉树的中序遍历序列和后序遍历序列构建出一棵树,然后你要输出这棵树的左视图和右视图。
输入格式:
输入第一行给出一个正整数 N (≤20),为树中的结点个数。随后在两行中先后给出树的中序遍历和后序遍历序列。树中所有键值都不相同,其数值大小无关紧要,都不超过 int 的范围。
输出格式:
第一行输出右视图,第二行输出左视图,格式如样例所示。
输入样例:
8
6 8 7 4 5 1 3 2
8 5 4 7 6 3 2 1
输出样例:
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++){//唯一的细节,找最左和最右
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;
}
版权声明
本文为[怀化第二深情]所创,转载请带上原文链接,感谢
https://blog.csdn.net/dege2929512534/article/details/124338712
边栏推荐
- 耳穴减肥自身感受细节描述0422
- Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
- 洋桃电子STM32物联网入门30步笔记二、CubeIDE下载、安装、汉化、设置
- word加水印
- 洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解
- 完全二叉搜索树 (30 分)
- Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library
- 【58】最后一个单词的长度【LeetCode】
- form表单 post提交 数据量大的问题
- Flink SQL实现流批一体
猜你喜欢
Let the earth have less "carbon" and rest on the road
RPC过程
Flink SQL实现流批一体
Knowledge points and problem solutions related to information collection
让地球少些“碳”息 度能在路上
QT compilation qtxlsx Library
idea配置连接远程数据库MySQL,或者是Navicat连接远程数据库失败问题(已解决)
【深度好文】Flink SQL流批⼀体化技术详解(一)
汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
Qtablewidget header customization and beautification developed by pyqt5 (with source code download)
随机推荐
RPC过程
【深度好文】Flink SQL流批⼀体化技术详解(一)
PDF with watermark
对OutputStream类的flush()方法的误解
关于数组复制问题
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
Go语言自学系列 | golang嵌套结构体
Swagger document export custom V2 / API docs interception
LINQ Learning Series ----- 1.4 anonymous objects
Anonymous type (c Guide Basics)
SYS_ CONNECT_ BY_ Path (column, 'char') combined with start with connect by prior
Reference passing 1
Go语言自学系列 | golang结构体作为函数参数
Go语言自学系列 | golang方法
How much inventory recording does the intelligent system of external call system of okcc call center need?
Transformer XL: attention language modelsbbeyond a fixed length context paper summary
QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)
Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing
Go语言自学系列 | golang结构体的初始化
GUI编程简介 swing