当前位置:网站首页>Middle and rear binary tree
Middle and rear binary tree
2022-04-23 03:04:00 【Learning KL & TK】
// Romantic compassion
// Middle preface follow-up Build up trees
// Array write 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( Recursion to the current layer , mid by Currently, the order nodes in this tree are constructed ,hou The current post order traverses the node )
// TODO len The number of nodes currently building this tree
TreeNode* build(int mid[], int hou[], int len) {
if (len == 0) return NULL;
// TODO Find the root node
int tmp = hou[len - 1];
int index = -1;
for(int i = 0; i < len; i++) {
if (tmp == mid[i]) {
index = i;
break;
}
}
// todo Number of left subtrees index , Number of right subtrees len - index - 1
TreeNode* root = new TreeNode();
root->val = tmp;
// TODO Found root node index
// TODO 1. 0 ~ index - 1 The following table range of the left subtree in the middle order
// TODO 2. index + 1 ~ len - 1 In the middle order you The following table range of subtree
// tOdo 3. 0 ~ index - 1 The number of left subtrees in subsequent
// todo 4. index ~ len - 2
// TODO Build a left subtree
root->l = build(mid, hou, index);
// todo build uou subtree
// todo Move the array head back index + 1 position
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;
}
版权声明
本文为[Learning KL & TK]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230303365437.html
边栏推荐
- 使用split来解决“最常见的单词”问题
- Source Generator实战
- 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
- TP5 inherits base and uses the variables in base
- BLDC double closed loop (speed PI + current PI) Simulink simulation model
- Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)
- AC & A2C & A3C
- Centos7 install MySQL 8 0
- Laravel8- use JWT
- How to write the expected salary on your resume to double your salary during the interview?
猜你喜欢
It turns out that PID was born in the struggle between Lao wangtou and Lao sky
Cherno_ Game engine series tutorial (5): 101~
MAUI初体验:爽
TP5 customization in extend directory succeeded and failed. Return information
Xamarin效果第二十二篇之录音效果
C# 11 的这个新特性,我愿称之最强!
Er and eer models
tf. keras. layers. Density function
Detailed log display of openfeign call
Plug in for vscode
随机推荐
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (9)
TP5 inherits base and uses the variables in base
Distributed system services
基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
Tips in MATLAB
Basic workflow of CPU
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
Gavl021, gavl281, AC220V to 5v200ma small volume non isolated chip scheme
tf. keras. layers. Inputlayer function
Encapsulation of ele table
Redis data server / database / cache (2022)
JSON data text
Er and eer models
SQL statement - DDL
最通俗易懂的依赖注入之生命周期
Plug in for vscode
最通俗易懂的依赖注入之服务容器与作用域
如果通过 C# 实现对象的深复制 ?
Vs code setting line feed
Response processing of openfeign