当前位置:网站首页>中后二叉建树
中后二叉建树
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
边栏推荐
- Basic SQL (VIII) data update operation practice
- Huawei machine test question -- deformation of hj53 Yang Hui triangle
- 《信息系统项目管理师总结》第五章 项目质量管理
- AC380V drop 5v12v24v200ma, UHV non isolated chip IC scheme
- Distributed system services
- Kubernetes - Introduction to actual combat
- Openfeign service call
- tf. keras. layers. Inputlayer function
- tf. keras. layers. Embedding function
- Summary of software test interview questions
猜你喜欢
Blazor University (12)组件 — 组件生命周期
FileNotFoundError: [Errno 2] No such file or directory
tf. keras. layers. Embedding function
C# WPF UI框架MahApps切换主题
[format] simple output (2)
Array and collection types passed by openfeign parameters
Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
Cherno_ Game engine series tutorial (5): 101~
Openfeign timeout setting
Distributed system services
随机推荐
Shell script learning -- practical case
How to deploy a website with only a server and no domain name?
Kubernetes - Introduction to actual combat
TP5 multi conditional where query (using PHP variables)
基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
Laravel8- use JWT
C# 11 的这个新特性,我愿称之最强!
Openfeign details show
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
Kubernetes - detailed explanation of pod
AspNetCore配置多环境log4net配置文件
《信息系统项目管理师总结》第六章 项目人力资源管理
Redis data server / database / cache (2022)
BLDC double closed loop (speed PI + current PI) Simulink simulation model
Passing object type parameters through openfeign
In redis cluster, the master node fails, and the IP changes after the master-slave switch. The client does not need to deal with it
Summary of software test interview questions
JSON data text
MAUI初体验:爽
.NET7之MiniAPI(特别篇):.NET7 Preview3