当前位置:网站首页>Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)
Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)
2022-04-23 02:52:00 【S atur】
Code implementation :
#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]; // Root order found in traversal index
if(la < k) tree[root].lson = build(la, k-1, ra, ra+k-la-1); // Recursive left subtree
if(lb > k) tree[root].rson = build(k+1, lb, ra+k-la, rb-1); // Recursive right subtree
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
边栏推荐
- Classification of technology selection (2022)
- First knowledge of C language ~ branch statements
- Source code and some understanding of employee management system based on polymorphism
- How to solve the complexity of project document management?
- Modification du contenu de la recherche dans la boîte déroulante par PHP + MySQL
- Efficient music format conversion tool Music Converter Pro
- B blocks of the 46th ICPC Asian regional competition (Kunming)
- Cuisine leetcode
- PIP install shutil reports an error
- Slave should be able to synchronize with the master in tests/integration/replication-psync.tcl
猜你喜欢
Store consumption SMS notification template
Huawei machine test question -- deformation of hj53 Yang Hui triangle
Airtrack cracking wireless network password (Dictionary running method)
Efficient music format conversion tool Music Converter Pro
重大危险源企业如何保障年底前完成双预防机制数字化建设任务
国产轻量级看板式Scrum敏捷项目管理工具
Intelligent agricultural management model
The way to conquer C language
Encapsulate components such as pull-down menu based on ele
The interface request takes too long. Jstack observes the lock holding
随机推荐
工业互联网+危化安全生产综合管理平台怎样建
能做多大的单片机项目程序开发,就代表了你的敲代码的水平
Shell script learning notes - regular expressions
First day of rhcsa
Rhcsa day 4 operation
The problem of removing spaces from strings
国产轻量级看板式Scrum敏捷项目管理工具
MySQL complex query uses temporary table / with as (similar to table variable)
The interface request takes too long. Jstack observes the lock holding
第46届ICPC亚洲区域赛(昆明) B Blocks(容斥+子集和DP+期望DP)
基于ele封装下拉菜单等组件
Rhcsa day 1 operation
PIP install shutil reports an error
JZ35 replication of complex linked list
魔王冷饭||#078 魔王答上海、南京行情;沟通指导;得国和打杀筛选;赚钱的目的;改变别人看法
Classification and regression tree of machine learning
《信息系统项目管理师总结》第六章 项目人力资源管理
C language 171 Number of recent palindromes
Log cutting - build a remote log collection server
Error installing Mongo service 'mongodb server' on win10 failed to start