当前位置:网站首页>[PTA] l2-011 play with binary tree
[PTA] l2-011 play with binary tree
2022-04-23 20:23:00 【Maybe】
Original link
Their thinking
Make use of the characteristics of preorder traversal and middle order traversal for deep search
Deep search exit conditions : Middle Preface >= The tail does not hold
Deep search recursion : Change the root node , The beginning and end of the middle order point to , Store subscript
The main points of : Find the root of the middle order from the previous order
ac Code
#include <iostream>
#include <cstring>
using namespace std;
int in[30], pre[30];
int level[100000];// The array should be a little larger , Because it's not a complete binary tree
void dfs(int root, int start, int end, int index) {
//root The first of preorder traversal start,end The beginning and end of middle order traversal index Stored subscript
if(start > end) return;
level[index] = pre[root];// this ( Son ) Root node of tree , Store
int i = start;
while(i < end && in[i] != pre[root]) i++;// Find the root node in the middle order traversal ( This is also the separation point of the left and right subtrees of the preorder traversal )
dfs(root + 1, start, i - 1, 2 * index + 2);// Here, the node is cleverly changed , Change the left and right nodes .
dfs(root + (i - start) + 1, i + 1, end, 2 * index + 1);
}
int main() {
memset(level, -1, sizeof(level));
int n;
scanf("%d",&n);
for(int i = 0; i < n; i++) scanf("%d", &in[i]);
for(int i = 0; i < n; i++) scanf("%d", &pre[i]);
dfs(0, 0, n - 1, 0);
int cnt = 0;
for(int i = 0; i < 10000; i++) {
if(level[i] != -1) {
if(cnt != n - 1) {
printf("%d ", level[i]);
cnt++;
} else{
printf("%d", level[i]);
break;
}
}
}
return 0;
}
版权声明
本文为[Maybe]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232020000253.html
边栏推荐
- Click an EL checkbox to select all questions
- 【PTA】L2-011 玩转二叉树
- Scripy tutorial - (2) write a simple crawler
- Remote code execution in Win 11 using wpad / PAC and JScript
- . Ren -- the intimate artifact in the field of vertical Recruitment!
- Commit and rollback in DCL of 16 MySQL
- Is the wechat CICC wealth high-end zone safe? How to open an account for securities
- Identification of bolt points in aerial photography based on perception
- Local call feign interface message 404
- Historical track data reading of Holux m1200-e Bluetooth GPS track recorder
猜你喜欢
PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)
A useless confession artifact
Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
Sqoop imports tinyint type fields to boolean type
16MySQL之DCL 中 COMMIT和ROllBACK
Monte Carlo py solves the area problem! (save pupils Series)
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
【PTA】整除光棍
随机推荐
After route link navigation, the sub page does not display the navigation style problem
Automatically fill in body temperature and win10 task plan
NC basic usage 4
Analysis of the relationship between generalized Bim and CAD under the current background
堡垒机、跳板机JumpServer的搭建,以及使用,图文详细
Remote code execution in Win 11 using wpad / PAC and JScript 1
R language uses the preprocess function of caret package for data preprocessing: BoxCox transform all data columns (convert non normal distribution data columns to normal distribution data and can not
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
Livego + ffmpeg + RTMP + flvjs to realize live video
JDBC tool class jdbcfiledateutil uploads files and date format conversion, including the latest, simplest and easiest way to upload single files and multiple files
Change the material of unity model as a whole
JDBC tool class jdbcconutil gets the connection to the database
PostgreSQL basic functions
R language ggplot2 visualization: ggplot2 visualizes the scatter diagram and uses geom_ mark_ The ellipse function adds ellipses around data points of data clusters or data groups for annotation
XXXI` Prototype ` displays prototype properties and`__ proto__` Implicit prototype properties
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127
Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
redis 分布式锁
go-zero框架数据库方面避坑指南