当前位置:网站首页>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
边栏推荐
- Configuring Apache Web services for servers such as Tianyi cloud
- Passing object type parameters through openfeign
- Judge whether there is a leap year in the given year
- 使用栈来解决”迷你语法分析器“的问题
- LNMP MySQL allows remote access
- Sonic cloud real machine tutorial
- Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
- Blazor University (12)组件 — 组件生命周期
- Introduction to ACM [inclusion exclusion theorem]
- Traversée de l'arbre L2 - 006
猜你喜欢

最通俗易懂的依赖注入之生命周期

基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?

Blazor University (12)组件 — 组件生命周期

Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system

Laravel new route file

REINFORCE

Summary of interface automation interview questions for software testing

Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)

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

Cherno_ Game engine series tutorial (5): 101~
随机推荐
腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
tf. keras. layers. Conv? D function
Tips in MATLAB
【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
How to use C language to realize [guessing numbers game]
HLS / chisel uses CORDIC hyperbolic system to realize square root calculation
最通俗易懂的依赖注入之生命周期
Microservices (distributed architecture)
Basic SQL (VIII) data update operation practice
Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
Table space capacity query and expansion of Oracle Database
Summary of software test interview questions
一套关于 内存对齐 的C#面试题,做错的人很多!
.NET点滴:说说Middleware构造中获取不到Scoped服务的问题
腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
[software testing] understand the basic knowledge of software testing
Blazor University (12)组件 — 组件生命周期
《信息系统项目管理师总结》第四章 项目成本管理
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)
Résumé du gestionnaire de projet du système d'information Chapitre VI gestion des ressources humaines du projet