当前位置:网站首页>L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
2022-04-23 02:52: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://yzsam.com/2022/04/202204230250562858.html
边栏推荐
- 《信息系统项目管理师总结》第七章 项目沟通管理
- 重大危险源企业如何保障年底前完成双预防机制数字化建设任务
- JZ35 replication of complex linked list
- The penultimate K nodes in jz22 linked list
- MySQL / SQL Server判断表或临时表存在则删除
- OCR recognition PDF file
- Codeforces Round #784 (Div. 4) (A - H)题解
- 【unity3D】直播间滚动式弹幕效果
- Classification of technology selection (2022)
- JZ22 鏈錶中倒數最後k個結點
猜你喜欢

基于多态的职工管理系统源码与一些理解

ele之Table表格的封装
![[wechat applet] set the bottom menu (tabbar) for the applet](/img/e2/98711dfb1350599cbdbdf13508b84f.png)
[wechat applet] set the bottom menu (tabbar) for the applet

JVM runtime data area (I)

Those years can not do math problems, using pyhon only takes 1 minute?

C language 171 Number of recent palindromes

基于Scrum进行创新和管理

How to build an integrated industrial Internet plus hazardous safety production management platform?

Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment

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
随机推荐
接口请求时间太长,jstack观察锁持有情况
JSON data text
OCR recognition PDF file
Centos7 install MySQL 8 0
Flink stream processing engine system learning (III)
First day of rhcsa
[learn junit5 from official documents] [II] [writingtests] [learning notes]
《信息系统项目管理师总结》第六章 项目人力资源管理
Navicat premium import SQL file
学习正则表达式选项、断言
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
字符串去掉空格问题
基于多态的职工管理系统源码与一些理解
期中汇总(概论+应用层+运输层)
Servlet template engine usage example
The problem of removing spaces from strings
Error installing Mongo service 'mongodb server' on win10 failed to start
windows MySQL8 zip安装
谷雨
Chapter V project quality management of information system project manager summary