当前位置:网站首页>L2-006 树的遍历(中后序确定二叉树&层序遍历)
L2-006 树的遍历(中后序确定二叉树&层序遍历)
2022-04-23 02:50: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://blog.csdn.net/Satur9/article/details/124357191
边栏推荐
- ROP Emporium x86_64 7~8题
- Mosaic Routing: implement / home / news
- mysql function函数语法
- Solve the problem that PowerShell mining occupies 100% of cpu7 in win7
- Shell script learning -- practical case
- OCR recognition PDF file
- JSON data text
- Classification of technology selection (2022)
- Kubernetes study notes
- How big the program development of single chip microcomputer project can be, it represents your level of knocking code
猜你喜欢

ROP Emporium x86_64 7~8题

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

Fashion MNIST 数据集分类训练

Store consumption SMS notification template

基于Torchserve部署SBERT模型<语义相似度任务>

How to solve the complexity of project document management?

leangoo脑图-共享式多人协作思维导图工具分享

Actual combat of industrial defect detection project (IV) -- ceramic defect detection based on hrnet

JVM类加载器

How to build an integrated industrial Internet plus hazardous safety production management platform?
随机推荐
Practical combat of industrial defect detection project (II) -- steel surface defect detection based on deep learning framework yolov5
C语言 171. 最近回文数
Planning code ROS migration POMDP prediction planning (I)
学习正则表达式选项、断言
《信息系统项目管理师总结》第四章 项目成本管理
The interface request takes too long. Jstack observes the lock holding
Microservices (distributed architecture)
[XJTU computer network security and management] Lecture 2 password technology
JVM runtime data area (I)
JVM运行时数据区(一)
Win view port occupation command line
高效音乐格式转换工具Music Converter Pro
Codeforces round 784 (Div. 4) (a - H)
ROP Emporium x86_ 64 7 ~ 8 questions
【Hcip】OSPF常用的6种LSA详解
Source code and some understanding of employee management system based on polymorphism
Looking for a job, writing a resume to an interview, this set of information is enough!
Codeforces Round #784 (Div. 4) (A - H)题解
Centos7 install MySQL 8 0
Navicat failed to connect to Oracle Database: cannot load OCI DLL, 87: instant client package is