当前位置:网站首页>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
边栏推荐
- 【工欲善其事必先利其器】论文编辑及文献管理(Endnote,Latex,JabRef ,overleaf)资源下载及使用指南
- JVM runtime data area (I)
- Learn regular expression options, assertions
- Looking for a job, writing a resume to an interview, this set of information is enough!
- C语言 171. 最近回文数
- C language 171 Number of recent palindromes
- Suggestion: block reference sorting is in the order of keywords
- 解决win7 中powershell挖矿占用CPU100%
- If MySQL / SQL server judges that the table or temporary table exists, it will be deleted
- The express project changes the jade template to art template
猜你喜欢
Android high-level interview must ask: overall business and project architecture design and reconstruction
Modify the content of MySQL + PHP drop-down box
LeetCode 1450 - 1453
C语言 171. 最近回文数
[hcip] detailed explanation of six LSAS commonly used by OSPF
Rhcsa day 1 operation
Efficient music format conversion tool Music Converter Pro
Preliminary understanding of stack and queue
Leangoo brain map - shared multi person collaborative mind mapping tool
Looking for a job, writing a resume to an interview, this set of information is enough!
随机推荐
OCR recognition PDF file
VirtualBox virtual machine (Oracle VM)
Modify the content of MySQL + PHP drop-down box
JS using the parameters of art template
Microservices (distributed architecture)
Chapter VII project communication management of information system project manager summary
Redis data server / database / cache (2022)
工业互联网+危化安全生产综合管理平台怎样建
Jz76 delete duplicate nodes in linked list
[hcip] detailed explanation of six LSAS commonly used by OSPF
Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment
Implementation of distributed scenario business operation log (based on redis lightweight)
grain rain
Interim summary (Introduction + application layer + transportation layer)
Learn regular expression options, assertions
《信息系統項目管理師總結》第六章 項目人力資源管理
Interpretation of the future development of smart agriculture
JZ76 删除链表中重复的结点
Rhcsa second day operation
5W of knowledge points