当前位置:网站首页>L2-006 树的遍历(中后序确定二叉树&层序遍历)
L2-006 树的遍历(中后序确定二叉树&层序遍历)
2022-04-23 02:50:00 【S atur】
代码实现:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e5+10;
int n, cnt;
int bk[N], md[N];
map<int, int> pos;
struct node{
int lson, rson, val;
}tree[N];
int build(int la, int lb, int ra, int rb){
int root = bk[rb];
int k = pos[root]; // 找到根节点在中序遍历中的index
if(la < k) tree[root].lson = build(la, k-1, ra, ra+k-la-1); // 递归左子树
if(lb > k) tree[root].rson = build(k+1, lb, ra+k-la, rb-1); // 递归右子树
return root;
}
void bfs(int root){
queue<int> q;
vector<int> ans;
q.push(root);
while(q.size()){
int t = q.front(); q.pop();
ans.push_back(t);
if(tree[t].lson) q.push(tree[t].lson);
if(tree[t].rson) q.push(tree[t].rson);
}
cout << ans[0];
for(int i = 1; i < ans.size(); i++) cout << " " << ans[i];
cout << endl;
}
signed main()
{
cin >> n;
for(int i = 1; i <= n; i ++) cin >> bk[i];
for(int i = 1; i <= n; i ++){
cin >> md[i];
pos[md[i]] = i;
}
int root = build(1, n, 1, n);
bfs(root);
return 0;
}
版权声明
本文为[S atur]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Satur9/article/details/124357191
边栏推荐
- 高效音乐格式转换工具Music Converter Pro
- 基于Scrum进行创新和管理
- MySQL insert free column
- Codeforces Round #784 (Div. 4) (A - H)题解
- [if you want to do a good job, you must first use its tools] Guide for downloading and using paper editing and document management (endnote, latex, jabref, overflow) resources
- TypeScript(1)
- Domestic lightweight Kanban scrum agile project management tool
- Airtrack cracking wireless network password (Dictionary running method)
- Renesas electronic MCU RT thread development and Design Competition
- 解决win7 中powershell挖矿占用CPU100%
猜你喜欢
Efficient music format conversion tool Music Converter Pro
解决win7 中powershell挖矿占用CPU100%
Source code and some understanding of employee management system based on polymorphism
Solve the problem that PowerShell mining occupies 100% of cpu7 in win7
Linux Redis ——Redis HA Sentinel 集群搭建详解 & Redis主从部署
Kubernetes - Introduction to actual combat
Android 高阶面试必问:全局业务和项目的架构设计与重构
Preliminary understanding of stack and queue
Flink stream processing engine system learning (I)
Flink learning (XI) watermark
随机推荐
The shell monitors the depth of the IBM MQ queue and scans it three times in 10s. When the depth value exceeds 5 for more than two times, the queue name and depth value are output.
打靶narak
How can enterprises with major hazard installations ensure the completion of the digital construction task of double prevention mechanism by the end of the year
Fashion MNIST dataset classification training
Jz76 delete duplicate nodes in linked list
leetcode 烹饪料理
ROP Emporium x86_64 7~8题
The second day of learning rhcsa
Rhcsa day 1 operation
Shell script learning notes - regular expressions
Kubernetes - Introduction to actual combat
@Usage and difference between mapper and @ repository
How to solve the complexity of project document management?
Fashion MNIST 数据集分类训练
The usage of case when and select case when is very easy to use
Leangoo brain map - shared multi person collaborative mind mapping tool
PIP install shutil reports an error
Implementation of distributed scenario business operation log (based on redis lightweight)
Domestic lightweight Kanban scrum agile project management tool
JZ35 replication of complex linked list