当前位置:网站首页>【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
边栏推荐
- 中金财富公司怎么样,开户安全吗
- Cadence Orcad Capture CIS更换元器件之Link Database 功能介绍图文教程及视频演示
- Mysql database backup scheme
- 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
- How to create bep-20 pass on BNB chain
- 2022 - Data Warehouse - [time dimension table] - year, week and holiday
- [talkative cloud native] load balancing - the passenger flow of small restaurants has increased
- Markdown < a > tag new page open link
- Remote code execution in Win 11 using wpad / PAC and JScript 1
- SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
猜你喜欢

Click an EL checkbox to select all questions

Understanding various team patterns in scrum patterns

Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report

Servlet learning notes

JDBC tool class jdbcfiledateutil uploads files and date format conversion, including the latest, simplest and easiest way to upload single files and multiple files

Numpy mathematical function & logical function

波场DAO新物种下场,USDD如何破局稳定币市场?
Handwritten Google's first generation distributed computing framework MapReduce

Openharmony open source developer growth plan, looking for new open source forces that change the world!

SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
随机推荐
Solution to PowerDesigner's failure to connect to MySQL in x64 system
An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue
还在用 ListView?使用 AnimatedList 让列表元素动起来
Mysql database - single table query (I)
R语言使用timeROC包计算存在竞争风险情况下的生存资料多时间AUC值、使用cox模型、并添加协变量、R语言使用timeROC包的plotAUCcurve函数可视化多时间生存资料的AUC曲线
The second method of file upload in form form is implemented by fileitem class, servletfileupload class and diskfileitemfactory class.
Intersection calculation of straight line and plane in PCL point cloud processing (53)
如何做产品创新?——产品创新方法论探索一
How to create bep-20 pass on BNB chain
Rédaction de thèses 19: différences entre les thèses de conférence et les thèses périodiques
Openharmony open source developer growth plan, looking for new open source forces that change the world!
Fundamentals of programming language (2)
STM32基础知识
Still using listview? Use animatedlist to make list elements move
JDBC tool class jdbcconutil gets the connection to the database
DNS cloud school | analysis of hidden tunnel attacks in the hidden corner of DNS
nc基础用法3
Design of warehouse management database system
An error is reported in the initialization metadata of the dolphin scheduler -- it turns out that there is a special symbol in the password. "$“
Alicloud: could not connect to SMTP host: SMTP 163.com, port: 25