当前位置:网站首页>L2-011 play binary tree (build tree + BFS)
L2-011 play binary tree (build tree + BFS)
2022-04-23 04:46:00 【Show Maker】
《 A very simple question 》
Just , If you know something about trees , So it's really simple . There are no difficult questions , You find it difficult because your knowledge is not proficient .
Full Score code
#include<bits/stdc++.h>
using namespace std;
const int N = 1e2+10;
int in[N],pre[N],n;
struct Node{
int l,r;
}Tree[N];
int build(int l1,int r1,int l2,int r2){
//l1-r1 In the sequence traversal ,l2-r2 The first sequence traversal
if(l1 > r1 || l2 > r2) return 0;
int p = l1;
while(in[p] != pre[l2]) p++;
int len = p - l1;
int rt = pre[l2];
Tree[rt].l = build(l1,p-1,l2+1,l2 + len);
Tree[rt].r = build(p+1,r1,l2+len+1,r2);
return rt;
}
vector<int> ans;
void bfs(int s){
queue<int> que;
que.push(s);
while(!que.empty()){
int rt = que.front();
que.pop();
ans.push_back(rt);
int l = Tree[rt].l,r = Tree[rt].r;
if(r) que.push(r);
if(l) que.push(l);
}
}
int main()
{
cin>>n;
for(int i = 1;i <= n; ++i) cin>>in[i];
for(int i = 1;i <= n; ++i) cin>>pre[i];
build(1,n,1,n);
bfs(pre[1]);
for(int i=0;i<ans.size();i++){
cout<<ans[i];
if(i!=ans.size()-1)cout<<" ";
}
return 0;
}
Build up trees
Building a tree is very simple , Simple structure , One l One r Just fine .
Ideas for building achievements :
1. First find the root of your current tree in the middle order
int p = l1;
while(in[p] != pre[l2]) p++;
The current tree refers to your current tree , Because then you may enter the subtree , Then the current tree becomes the subtree
And this root has been given out , It's you Of the current tree Of a pre ordered sequence first
2. Find the length of the left subtree
In fact, that is int len = p - l1
3. The left child and right child of the current root are solved recursively
The parameters here , These four are :
Current
The first index in the middle order , The last index in the middle order , The first index of the preamble , The last index of the preamble
int rt = pre[l2];
Tree[rt].l = build(l1,p-1,l2+1,l2 + len);
Tree[rt].r = build(p+1,r1,l2+len+1,r2);
return rt;
4. Special judgement , If you recurse down the current tree and find it illegal ( Left is greater than right ) Then it means that it should not recurse . It's a leaf node .
if(l1 > r1 || l2 > r2) return 0;
Code
int build(int l1,int r1,int l2,int r2){
//l1-r1 In the sequence traversal ,l2-r2 The first sequence traversal
if(l1 > r1 || l2 > r2) return 0;
int p = l1;
while(in[p] != pre[l2]) p++;
int len = p - l1;
int rt = pre[l2];
Tree[rt].l = build(l1,p-1,l2+1,l2 + len);
Tree[rt].r = build(p+1,r1,l2+len+1,r2);
return rt;
}
Sequence traversal ( use bfs)
That means you ask me to . The last way , Positive solution , Namely bfs. because bfs It seems that by definition , Just layer by layer . In fact, it is the definition of complete sequence traversal .
Ideas :
Anyway, your tree has been built .
Then you put the root in first
After that, go in the order of first right node and then left node push go in
Go in this order bfs Just fine .
( Be careful , Because the leaf nodes are set on the left and right 0, So you need to judge , If it is 0 Words , The description is leaf node , Then you don't have to put it in )
Code
void bfs(int s){
queue<int> que;
que.push(s);
while(!que.empty()){
int rt = que.front();
que.pop();
ans.push_back(rt);
int l = Tree[rt].l,r = Tree[rt].r;
if(r) que.push(r);
if(l) que.push(l);
}
}
版权声明
本文为[Show Maker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230442591971.html
边栏推荐
- Raspberry pie + opencv + opencv -- face detection ------- environment construction
- [timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
- Practice and exploration of knowledge map visualization technology in meituan
- What is a blocking queue? What is the implementation principle of blocking queue? How to use blocking queue to implement producer consumer model?
- The unity camera rotates with the mouse
- Redis command Encyclopedia
- 做数据可视化应该避免的8个误区
- Leetcode009 -- search the target value in the array with binary search
- Wechat payment function
- Leetcode - > 1 sum of two numbers
猜你喜欢

/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)

Recursive call -- Enumeration of permutations
![[paper reading] [3D object detection] voxel transformer for 3D object detection](/img/a2/9f66789cc12fad99491309717cf418.png)
[paper reading] [3D object detection] voxel transformer for 3D object detection

2021数学建模国赛一等奖经验总结与分享

Recommended scheme for national production of electronic components for wireless charging

Learning Android from scratch -- Introduction

Key points of AWS eks deployment and differences between console and eksctl creation

QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效

Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience

Pixel mobile phone brick rescue tutorial
随机推荐
Create VPC in AWS console (no plate)
Recursive call -- Enumeration of permutations
No such file or directory problem while executing shell
Leetcode004 -- Roman numeral to integer
Jetpack -- lifecycle usage and source code analysis
Pixel 5 5g unlocking tutorial (including unlocking BL, installing edxposed and root)
Innovation training (VI) routing
Excel protects worksheets and workbooks from damage
Eight misunderstandings that should be avoided in data visualization
test
Leetcode005 -- delete duplicate elements in the array in place
Introduction to raspberry pie 3B - system installation
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
Improving 3D object detection with channel wise transformer
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
QML advanced (V) - realize all kinds of cool special effects through particle simulation system
Differences among electric drill, electric hammer and electric pick
La caméra Unity tourne avec la souris
Innovation training (II) task division
Flink's important basics