当前位置:网站首页>中后二叉建树
中后二叉建树
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
边栏推荐
- Kubernetes study notes
- 最通俗易懂的依赖注入之生命周期
- Typescript Learning Guide
- tf. keras. layers. Density function
- Onenet connection process
- Chapter VII project communication management of information system project manager summary
- Q-Learning & Sarsa
- Laravel's own paging query
- tf. keras. layers. Inputlayer function
- Opencv reads webcam video and saves it locally
猜你喜欢

Plug in for vscode

微软是如何解决 PC 端程序多开问题的

Laravel new route file

Sonic cloud real machine tutorial

Deep q-network (dqn)

腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元

Liunx foundation - zabbix5 0 monitoring system installation and deployment

Configuring Apache Web services for servers such as Tianyi cloud

Judge whether there is a leap year in the given year

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
随机推荐
Opencv fills the rectangle with a transparent color
Winsock programming interface experiment: implementation of ipconfig
MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator
基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
Distributed system services
对.NET未来的一点感悟
利用栈的回溯来解决“文件的最长绝对路径”问题
腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
Basic SQL (VIII) data update operation practice
tf. keras. layers. Timedistributed function
Laravel's own paging query
L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
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
REINFORCE
Depth deterministic strategy gradient (ddpg)
Opencv reads webcam video and saves it locally
Blazor University (11)组件 — 替换子组件的属性
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
Kubernetes - detailed explanation of pod
.NET7之MiniAPI(特别篇):.NET7 Preview3