当前位置:网站首页>[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
边栏推荐
- Customize timeline component styles
- Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
- Cadence OrCAD capture batch change component packaging function introduction graphic tutorial and video demonstration
- Cadence Orcad Capture CIS更换元器件之Link Database 功能介绍图文教程及视频演示
- Use the rolling division method to find the maximum common divisor of two numbers
- 堡垒机、跳板机JumpServer的搭建,以及使用,图文详细
- NC basic usage 4
- R language survival package coxph function to build Cox regression model, ggrisk package ggrisk function and two_ Scatter function visualizes the risk score map of Cox regression, interprets the risk
- Markdown < a > tag new page open link
- PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)
猜你喜欢
DTMF双音多频信号仿真演示系统
Scrapy教程 - (2)寫一個簡單爬蟲
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
What is the difference between a host and a server?
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
波场DAO新物种下场,USDD如何破局稳定币市场?
Computing the intersection of two planes in PCL point cloud processing (51)
Click an EL checkbox to select all questions
Livego + ffmpeg + RTMP + flvjs to realize live video
PCL点云处理之计算两平面交线(五十一)
随机推荐
WordPress插件:WP-China-Yes解决国内访问官网慢的方法
Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
Numpy sort search count set
Implementation of mypromise
Numpy mathematical function & logical function
PCL点云处理之直线与平面的交点计算(五十三)
Record: call mapper to report null pointer Foreach > the usage of not removing repetition;
考研英语唐叔的语法课笔记
ABAQUS script email auto notification
Servlet learning notes
R语言survival包coxph函数构建cox回归模型、ggrisk包ggrisk函数和two_scatter函数可视化Cox回归的风险评分图、解读风险评分图、基于LIRI数据集(基因数据集)
What is the difference between a host and a server?
Rédaction de thèses 19: différences entre les thèses de conférence et les thèses périodiques
Cadence Orcad Capture CIS更换元器件之Link Database 功能介绍图文教程及视频演示
JDBC database addition, deletion, query and modification tool class
一. js的深拷贝和浅拷贝
三十一. `prototype`显示原型属性和`__proto__`隐式原型属性
Devexpress 14.1 installation record
An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue
Cadence Orcad Capture 批量更改元件封装功能介绍图文教程及视频演示