当前位置:网站首页>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
边栏推荐
- Innovation training (10)
- leetcode006--查找字符串数组中的最长公共前缀
- QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
- Leetcode006 -- find the longest common prefix in the string array
- Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
- Improving 3D object detection with channel wise transformer
- Unity3d practical skills - theoretical knowledge base (I)
- Arduino UNO r3+LCD1602+DHT11
- Spark optimization
- Installation and deployment of Flink and wordcount test
猜你喜欢

Wechat payment function

Introduction to raspberry pie 3B - system installation

Installation and use of Apache bench (AB pressure test tool)

使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘

Excel uses the functions of replacement, sorting and filling to comprehensively sort out financial data

MySQL queries users logged in for at least N consecutive days

Summary of MySQL de duplication methods

C language: Advanced pointer

520. Detect capital letters

Eight misunderstandings that should be avoided in data visualization
随机推荐
Special topic of data intensive application system design
The unity camera rotates with the mouse
Innovation training (XI) airline ticket crawling company information
leetcode003--判断一个整数是否为回文数
用LCR表完美测试无线充电系统中的线圈
No such file or directory problem while executing shell
Unity rawimage background seamlessly connected mobile
Last day of 2017
List remove an element
2021数学建模国赛一等奖经验总结与分享
C language: spoof games
Migrate from MySQL database to AWS dynamodb
Innovation training (XII) reptile
[paper reading] [3D target detection] point transformer
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
PHP+MySQL 制作留言板
C language: Advanced pointer
使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘
Manually write smart pointer shared_ PTR function
KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘