当前位置:网站首页>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
边栏推荐
- Leetcode002 -- inverts the numeric portion of a signed integer
- IDE idea automatic compilation and configuration of on update action and on frame deactivation
- Leetcode - > 1 sum of two numbers
- Coinbase: basic knowledge, facts and statistics about cross chain bridge
- Unity RawImage背景无缝连接移动
- JS détermine si la chaîne de nombres contient des caractères
- 做数据可视化应该避免的8个误区
- Introduction to raspberry pie 3B - system installation
- Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
- Spark FAQ sorting - must see before interview
猜你喜欢

Record the ThreadPoolExecutor main thread waiting for sub threads

拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!

补充番外14:cmake实践项目笔记(未完待续4/22)

Arduino UNO r3+LCD1602+DHT11
![[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

Perfect test of coil in wireless charging system with LCR meter

Spark small case - RDD, spark SQL

Innovation training (IX) integration

Recommended scheme of national manufactured electronic components

做数据可视化应该避免的8个误区
随机推荐
Teach you how to build the ruoyi system by Tencent cloud
Mysql, binlog log query
[paper reading] [3D object detection] voxel transformer for 3D object detection
泰克示波器DPO3054自校准SPC失败维修
Leetcode001 -- returns the subscript of the array element whose sum is target
leetcode009--用二分查找在数组中搜索目标值
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
List&lt; Map&gt; Replication: light copy and deep copy
The programmer starts the required application with one click of window bat
What's the difference between error and exception
Spark case - wordcount
Gets all dates between two times
js 判断数字字符串中是否含有字符
Custom switch control
Migrate from MySQL database to AWS dynamodb
Leetcode003 -- judge whether an integer is a palindrome number
New terminal play method: script guidance independent of technology stack
redis数据类型有哪些
Open the past and let's start over.
Getprop property