当前位置:网站首页>【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
边栏推荐
- Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
- R语言ggplot2可视化:ggplot2可视化散点图并使用geom_mark_ellipse函数在数据簇或数据分组的数据点周围添加椭圆进行注释
- . Ren -- the intimate artifact in the field of vertical Recruitment!
- 网络通信基础(局域网、广域网、IP地址、端口号、协议、封装、分用)
- 【问题解决】‘ascii‘ codec can‘t encode characters in position xx-xx: ordinal not in range(128)
- Mysql database - connection query
- R language uses econocrats package to create microeconomic or macroeconomic map, visualize indifference function indifference curve, customize calculation intersection, and customize the parameters of
- Redis的安装(CentOS7命令行安装)
- nc基础用法
- 腾讯邱东洋:深度模型推理加速的术与道
猜你喜欢
Wave field Dao new species end up, how does usdd break the situation and stabilize the currency market?
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
go-zero框架数据库方面避坑指南
16MySQL之DCL 中 COMMIT和ROllBACK
After route link navigation, the sub page does not display the navigation style problem
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
随机推荐
Servlet learning notes
Remote code execution in Win 11 using wpad / PAC and JScript
Grafana shares links with variable parameters
R语言ggplot2可视化:ggplot2可视化散点图并使用geom_mark_ellipse函数在数据簇或数据分组的数据点周围添加椭圆进行注释
PCL点云处理之基于PCA的几何形状特征计算(五十二)
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
DNS cloud school rising posture! Three advanced uses of authoritative DNS
Redis distributed lock
NC basic usage 3
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
Recommend an open source free drawing software draw IO exportable vector graph
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
Click an EL checkbox to select all questions
nc基础用法1
How about CICC fortune? Is it safe to open an account
[problem solving] 'ASCII' codec can't encode characters in position XX XX: ordinal not in range (128)
Leetcode XOR operation
Remote code execution in Win 11 using wpad / PAC and JScript 1
Design of library management database system
Notes of Tang Shu's grammar class in postgraduate entrance examination English