当前位置:网站首页>中后二叉建树
中后二叉建树
2022-04-23 03:03:00 【学习kl&tk】
// 浪漫恻隐
// 中序 后续 建树
// 数组写 https://blog.csdn.net/qq_33957603/article/details/117093898?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165028864316780261969875%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=165028864316780261969875&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-6-117093898.142^v9^pc_search_result_control_group,157^v4^control&utm_term=%E6%B5%AA%E6%BC%AB%E4%BE%A7%E5%BD%B1+pta&spm=1018.2226.3001.4187
#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode* l;
TreeNode* r;
};
// TODO(递归到当前这一层, mid 为 当前构建这颗树中序节点 ,hou 当前的后序遍历节点)
// TODO len 当前构建这颗树的节点个数
TreeNode* build(int mid[], int hou[], int len) {
if (len == 0) return NULL;
// TODO 找到根节点
int tmp = hou[len - 1];
int index = -1;
for(int i = 0; i < len; i++) {
if (tmp == mid[i]) {
index = i;
break;
}
}
// todo 左子树个数 index , 右子树个数 len - index - 1
TreeNode* root = new TreeNode();
root->val = tmp;
// TODO 找到了根节点 index
// TODO 1. 0 ~ index - 1 中序中左子树的下表范围
// TODO 2. index + 1 ~ len - 1 中序中you子树的下表范围
// tOdo 3. 0 ~ index - 1 后续中左子树的个数
// todo 4. index ~ len - 2
// TODO 建左子树
root->l = build(mid, hou, index);
// todo 建uou子树
// todo 将数组头向后挪 index + 1位
root->r = build(mid + index + 1, hou + index, len - index - 1);
return root;
}
int a[25], b[25];
vector<int> ans1, ans2;
void bfs(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
int len = q.size();
for(int i = 0;i < len; i++) {
TreeNode* tmp = q.front();
q.pop();
// cout << root->val << endl;
if (tmp->l != NULL) q.push(tmp->l);
if (tmp->r != NULL) q.push(tmp->r);
if (i == 0) ans1.push_back(tmp->val);
if(i == len - 1) ans2.push_back(tmp->val);
}
}
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < n; i++) cin >> b[i];
TreeNode* root = build(a, b, n);
bfs(root);
cout << "R:";
for(auto i : ans2) {
cout << " " << i;
}
cout << endl;
cout << "L:";
for(auto i : ans1) {
cout << " " << i;
}
cout << endl;
return 0;
}
版权声明
本文为[学习kl&tk]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_53013914/article/details/124325755
边栏推荐
- The space between the left and right of the movie ticket seats is empty and cannot be selected
- Realize QQ login with PHP
- c#可变参数params的介绍
- Xamarin效果第二十二篇之录音效果
- Q-Learning & Sarsa
- Systemctl start Prometheus + grafana environment
- AC380V drop 5v12v24v200ma, UHV non isolated chip IC scheme
- Gavl021, gavl281, AC220V to 5v200ma small volume non isolated chip scheme
- MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator
- MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
猜你喜欢

Kubernetes - Introduction to actual combat

Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s

【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持

C# WPF UI框架MahApps切换主题

Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
![Introduction to ACM [TSP problem]](/img/9f/4e3592542d989b2fbb6d82f7f2fbd2.png)
Introduction to ACM [TSP problem]

C#中切片语法糖的使用

How to write the expected salary on your resume to double your salary during the interview?

Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)

ASP.NET 6 中间件系列 - 执行顺序
随机推荐
基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
Chapter VII project communication management of information system project manager summary
最通俗易懂的依赖注入之服务容器与作用域
Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s
MYSQL05_ Ordr by sorting, limit grouping, group by grouping
Opencv combines multiple pictures into video
《信息系统项目管理师总结》第四章 项目成本管理
Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
Vs code setting line feed
《信息系统项目管理师总结》第六章 项目人力资源管理
tf. keras. layers. Inputlayer function
Opencv fills the rectangle with a transparent color
The difference between encodeuri and encodeuricomponent
tf. keras. layers. Embedding function
C# 读写二进制文件
Creating wechat voucher process with PHP
.NET7之MiniAPI(特别篇):.NET7 Preview3
L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
Configuring Apache Web services for servers such as Tianyi cloud
How to write the expected salary on your resume to double your salary during the interview?