当前位置:网站首页>【PTA】L2-011 玩转二叉树
【PTA】L2-011 玩转二叉树
2022-04-23 20:20:00 【也许会吧】
原题链接
解题思路
利用前序遍历和中序遍历的特点进行深搜
深搜退出条件:中序首>=尾不成立
深搜递归:改变根节点,中序首尾指向,存放下标
要点:由前序找到中序的根
ac代码
#include <iostream>
#include <cstring>
using namespace std;
int in[30], pre[30];
int level[100000];//数组要开大一点,因为不是完全二叉树
void dfs(int root, int start, int end, int index) {
//root前序遍历的首 start,end中序遍历的首尾 index存放的下标
if(start > end) return;
level[index] = pre[root];//此(子)树的根节点,储存
int i = start;
while(i < end && in[i] != pre[root]) i++;//找到中序遍历中的根节点 (这也是前序遍历的左右子树分开点)
dfs(root + 1, start, i - 1, 2 * index + 2);//这里巧妙的将结点换位置,将左边和右边结点换位置。
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;
}
版权声明
本文为[也许会吧]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_55475680/article/details/124355263
边栏推荐
- [target tracking] pedestrian attitude recognition based on frame difference method combined with Kalman filter, with matlab code
- 2022 - Data Warehouse - [time dimension table] - year, week and holiday
- Sqoop imports tinyint type fields to boolean type
- 【目标跟踪】基于帧差法结合卡尔曼滤波实现行人姿态识别附matlab代码
- The flinkcdc reports an error: but this is no longer available on the server
- 本地调用feign接口报404
- Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
- nc基础用法1
- R language uses timeroc package to calculate the multi time AUC value of survival data under competitive risk, uses Cox model and adds covariates, and R language uses the plotauccurve function of time
- Still using listview? Use animatedlist to make list elements move
猜你喜欢

What is the difference between a host and a server?

WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website

CVPR 2022 | querydet: use cascaded sparse query to accelerate small target detection under high resolution

MySQL advanced lock - overview of MySQL locks and classification of MySQL locks: global lock (data backup), table level lock (table shared read lock, table exclusive write lock, metadata lock and inte
![[graph theory brush question-4] force deduction 778 Swimming in a rising pool](/img/e3/a8cd9fc7773843e9e8ee6a6eba123f.png)
[graph theory brush question-4] force deduction 778 Swimming in a rising pool

Commit and ROLLBACK in DCL of 16mysql

Numpy mathematical function & logical function

Redis cache penetration, cache breakdown, cache avalanche

Fundamentals of programming language (2)

Tensorflow 2 basic operation dictionary
随机推荐
Intersection calculation of straight line and plane in PCL point cloud processing (53)
Operation of numpy array
Linux64Bit下安装MySQL5.6-不能修改root密码
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
How does onlyoffice solve no route to host
STM32基础知识
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
What is the difference between a host and a server?
JDBC tool class jdbcconutil gets the connection to the database
Numpy sort search count set
PCL点云处理之直线与平面的交点计算(五十三)
MySQL advanced lock - overview of MySQL locks and classification of MySQL locks: global lock (data backup), table level lock (table shared read lock, table exclusive write lock, metadata lock and inte
Why does ES6 need to introduce map when JS already has object type
[problem solving] 'ASCII' codec can't encode characters in position XX XX: ordinal not in range (128)
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
Sqoop imports data from Mysql to HDFS using lzop compression format and reports NullPointerException
[latex] 5 how to quickly write out the latex formula corresponding to the formula
R language ggplot2 visual facet_wrap, and use the lineheight parameter to customize the height of the facet icon tab (gray label bar)
Remote code execution in Win 11 using wpad / PAC and JScript