当前位置:网站首页>F - Pre-order and In-order(Atcoder 255)
F - Pre-order and In-order(Atcoder 255)
2022-07-27 03:38:00 【CTGU-Yoghurt】
题目:

题目链接: F - Pre-order and In-order (atcoder.jp)
题解:Editorial - Aising Programming Contest 2022(AtCoder Beginner Contest 255)
思路:
因为官方题解已经说的很清楚了
我这里就简要补充一些
1.首先大家要了解pre-order(先序)和in-order(中序)的概念
2.因为要构建的为一个满足题目中先序和中序确定的一个二叉树
我可以通过一个构建L[10006]和R[10006]的数组
分别存储L[i]和R[i]的左结点和右结点
3.边构造的时候边判断是否满足先序和中序
主要是判断是否结点越界的问题
代码详解:
#include<iostream>
using namespace std;
const int N = 200006;
int P[N], I[N], Iinv[N];
int L[N], R[N];
bool slove(int s,int e,int S,int E) {//s,e为在p中的范围,S,E为在i中的范围
int root = P[s], p = Iinv[root];//p为在中序列的位置
if (p<S || p>E) return 0;//结点不在构建的子树范围内
if (p - S > 0) {//能够构建左子树
L[root] = P[s + 1];//以p中s+1作为子树的根结点,并记录在L中
if (!slove(s + 1, e, S, p - 1)) return 0;
//更新[S,P-1]为作为新子树区间范围,更新[s+1,e]作为
}
if (E - p > 0) {//同上
R[root] = P[s+p-S+1];//p中的右子结点的第一个
if (!slove(s + p - S + 1, e, p + 1, E)) return 0;
}
return 1;
}
int main(){
int n;
cin >> n;
for (int i = 1; i <=n; i++) cin >> P[i];
for (int i = 1; i <=n; i++) cin >> I[i];
for (int i = 1; i <=n; i++) Iinv[I[i]]=i;
if (P[1] != 1 || !slove(1, n, 1, n)) {//1必须作为root
cout << -1 << endl;
return 0;
}
for (int i = 1; i <= n; i++) cout << L[i] << " " << R[i]<<endl;
return 0;
}
边栏推荐
- Big talk · book sharing | lean product development: principles, methods and Implementation
- Framework learning journey: init process startup process
- 微服务化解决文库下载业务问题实践
- CloudCompare&PCL 匹配点距离抑制
- 记一次TCP丢包带来的重大性能问题
- Project time zone problem solving
- Interview question 16.05 factorial mantissa
- Abstract intelligent extraction [based on Bert technology]
- 【LeetCode】Day104-无重叠区间
- What is animation effect? What is the transition effect?
猜你喜欢

什么是动画效果?什么是过渡效果?

356页14万字高端商业办公综合楼弱电智能化系统2022版

Five basic data structures of redis

influxDB 基础了解

【比赛参考】PyTorch常用代码段以及操作合集

H. 265 web player easyplayer's method of opening video to the public

Subject 3: Jinan Zhangqiu line 5

Worship the 321 page PDF of the core technology of Internet entrepreneurship that Alibaba is pushing internally. It's really kneeling

2022危险化学品生产单位安全生产管理人员考试题模拟考试题库模拟考试平台操作

Nonlinear optimal tracking control based on wind energy conversion system (realized by matlab code)
随机推荐
C get UUID
Subject 3: Jinan Zhangqiu line 5
Is VR panoramic production a single weapon in the home decoration industry? Why is this?
EVT interface definition file of spicy
[untitled]
xxx is not in the sudoers file.This incident will be reported.的解决方法
法解析的外部符号 “public: virtual __cdecl nvinfer1::YoloLayerPlugin::~YoloLayerPlugin(void)“ “public: virtua
An online duplicate of a hidden bug
DINO 论文精度,并解析其模型结构 & DETR 的变体
2022年危险化学品经营单位主要负责人复训题库及答案
JMeter学习笔记004-CSV文件行数控制循环次数
Interview question 02.05. sum of linked list
深度剖析 —— 动态内存管理
Nonlinear optimal tracking control based on wind energy conversion system (realized by matlab code)
卷积神经网络——灰度图像的卷积
2022 retraining question bank and answers for main principals of hazardous chemical business units
每日一题:奇偶树
Collating strings
Parallel desktop startup virtual machine "operation failed" problem solution
【比赛参考】PyTorch常用代码段以及操作合集