当前位置:网站首页>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
边栏推荐
- The second day of learning rhcsa
- 解决win7 中powershell挖矿占用CPU100%
- ROP Emporium x86_64 7~8题
- 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
- Essential qualities of advanced programmers
- 谷雨
- Solve the problem that PowerShell mining occupies 100% of cpu7 in win7
- Water diversion into chengluo Valley p1514
- Shell learning notes -- shell processing of output stream awk
- Servlet template engine usage example
猜你喜欢

能做多大的单片机项目程序开发,就代表了你的敲代码的水平

Store consumption SMS notification template

Flink stream processing engine system learning (II)

C语言 171. 最近回文数

Android high-level interview must ask: overall business and project architecture design and reconstruction

Machine learning (Zhou Zhihua) Chapter 14 probability graph model

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

Slave should be able to synchronize with the master in tests/integration/replication-psync.tcl

The way to conquer C language

ROP Emporium x86_64 7~8题
随机推荐
leangoo脑图-共享式多人协作思维导图工具分享
Encapsulate components such as pull-down menu based on ele
MySQL complex query uses temporary table / with as (similar to table variable)
Flink stream processing engine system learning (II)
基于ele封装下拉菜单等组件
Suggestion: block reference sorting is in the order of keywords
The problem of removing spaces from strings
The way to conquer C language
【unity3D】直播间滚动式弹幕效果
Les derniers noeuds K de la liste jz22
JZ22 鏈錶中倒數最後k個結點
Shell script learning notes -- shell operation on files sed
Rhcsa day 4 operation
VirtualBox virtual machine (Oracle VM)
grain rain
Flink stream processing engine system learning (I)
Solve the problem that PowerShell mining occupies 100% of cpu7 in win7
工业互联网+危化安全生产综合管理平台怎样建
Shell learning notes -- shell processing of output stream awk
The usage of case when and select case when is very easy to use