当前位置:网站首页>L2-011 玩转二叉树(建树+BFS)
L2-011 玩转二叉树(建树+BFS)
2022-04-23 04:43:00 【Show Maker】
《非常简单的一道题目》
就,如果你懂得关于树的知识,那么就确实就是很简单。题目都没有很难的,你觉得难是因为你的知识点不熟练。
满分代码
#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中序遍历,l2-r2先序遍历
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;
}
建树
建树就非常简单,就用结构体,一个l一个r就好了。
建树的思路:
1.先在中序中找到你当前树的根
int p = l1;
while(in[p] != pre[l2]) p++;
当前树指的是你当前树,因为之后可能会进入子树,那么当前树就变成了那个子树
而这个根已经给出来了,其实就是你当前树的先序序列的第一个
2.求出左子树的长度
其实就是int len = p - l1
3.当前根的左孩子和右孩子递归求解
这里的参数呢,这四个分别是:
当前的
中序的第一个索引,中序的最后一个索引,先序的第一个索引,先序的最后一个索引
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.特判,如果说你当前树递归下去发现了不合法(左大于右)那么就说明它不应该递归下去。它是一个叶子结点。
if(l1 > r1 || l2 > r2) return 0;
代码
int build(int l1,int r1,int l2,int r2){
//l1-r1中序遍历,l2-r2先序遍历
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;
}
层序遍历(用bfs)
就是说你要求层序遍历。最后的方法,正解,就是bfs。因为bfs好像从定义来看,就是一层一层地。其实就是完完全全的层序遍历的定义啊。
思路:
反正你的树已经建好了嘛。
那么你先把根放进去
之后按照先右结点再左结点的顺序去push进去
按照这个顺序去bfs就好了。
(注意,因为上面设置了叶子结点的左右都是0,所以你需要判断一下,如果是0的话,说明是叶子结点,那就不用放进去了)
代码
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://blog.csdn.net/weixin_45564209/article/details/124350188
边栏推荐
- Interaction of diet gut microbiota on cardiovascular disease
- The perfect combination of collaborative process and multi process
- 2021数学建模国赛一等奖经验总结与分享
- A new method for evaluating the quality of metagenome assembly - magista
- 做数据可视化应该避免的8个误区
- AWS EKS 部署要点以及控制台与eksctl创建的差异
- Record the blind injection script
- Alibaba tip: it is better to create threads manually
- leetcode007--判断字符串中的括号是否匹配
- Bridge between ischemic stroke and intestinal flora: short chain fatty acids
猜你喜欢

Spark FAQ sorting - must see before interview

Supplément: annotation

QML advanced (IV) - drawing custom controls

Practice and exploration of knowledge map visualization technology in meituan

zynq平臺交叉編譯器的安裝

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

补:注解(Annotation)

Recommended scheme for national production of electronic components of wireless keyboard

简单的拖拽物体到物品栏

Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
随机推荐
Use recyclerview to realize left-right side-by-side classification selection
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
Redis 命令大全
Installation of zynq platform cross compiler
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
New terminal play method: script guidance independent of technology stack
Basic use of shell WC (counting the number of characters)
Installation du compilateur croisé de la plateforme zynq
Differences among electric drill, electric hammer and electric pick
leetcode004--罗马数字转整数
针对NFT的网络钓鱼
Logger and zap log Library in go language
Detailed explanation of life cycle component of jetpack
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
What is the thirty-six plan
Open the past and let's start over.
Small volume Schottky diode compatible with nsr20f30nxt5g
Simply drag objects to the item bar
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN