当前位置:网站首页>Traversée de l'arbre L2 - 006
Traversée de l'arbre L2 - 006
2022-04-23 02:52:00 【S atur】
Mise en œuvre du Code:
#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]; // Trouver le noeud racine index
if(la < k) tree[root].lson = build(la, k-1, ra, ra+k-la-1); // Sous - arbre récursif gauche
if(lb > k) tree[root].rson = build(k+1, lb, ra+k-la, rb-1); // Sous - arbre droit récursif
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://yzsam.com/2022/04/202204230250562858.html
边栏推荐
- Flink learning (XI) watermark
- Looking for a job, writing a resume to an interview, this set of information is enough!
- Huawei machine test question -- deformation of hj53 Yang Hui triangle
- Linux Redis ——Redis HA Sentinel 集群搭建详解 & Redis主从部署
- 基于ele封装下拉菜单等组件
- Centos7 install MySQL 8 0
- VirtualBox virtual machine (Oracle VM)
- L2-006 树的遍历(中后序确定二叉树&层序遍历)
- JVM runtime data area (I)
- Suggestion: block reference sorting is in the order of keywords
猜你喜欢
Encapsulation of ele table
Log cutting - build a remote log collection server
Six very 6 computer driver managers: what software is good for driver upgrade? Recommended by the best computer driver management software abroad
ROP Emporium x86_ 64 7 ~ 8 questions
Day 3 of learning rhcsa
Flink stream processing engine system learning (III)
Actual combat of industrial defect detection project (IV) -- ceramic defect detection based on hrnet
How to build an integrated industrial Internet plus hazardous safety production management platform?
L2-006 树的遍历(中后序确定二叉树&层序遍历)
php+mysql对下拉框搜索的内容修改
随机推荐
Store consumption SMS notification template
win查看端口占用 命令行
Redis data server / database / cache (2022)
Classification of technology selection (2022)
Devil cold rice 𞓜 078 devil answers the market in Shanghai and Nanjing; Communication and guidance; Winning the country and killing and screening; The purpose of making money; Change other people's op
MySQL function syntax
Essential qualities of advanced programmers
Winsock programming interface experiment: Ping
《信息系统项目管理师总结》第五章 项目质量管理
PIP install shutil reports an error
Shell script learning -- practical case
【Hcip】OSPF常用的6种LSA详解
基于多态的职工管理系统源码与一些理解
Linux Redis ——Redis HA Sentinel 集群搭建详解 & Redis主从部署
Rhcsa day 3 operation
VirtualBox virtual machine (Oracle VM)
How to solve the complexity of project document management?
JZ22 鏈錶中倒數最後k個結點
Solve the problem that PowerShell mining occupies 100% of cpu7 in win7
Log4j知识点记录