当前位置: 首页 > news >正文

优选算法——队列+BFS

目录

1. N叉树的层序遍历

2.  二叉树的锯齿层序遍历

3. 二叉树最大宽度

4. 在每个树行中找最大值


1. N叉树的层序遍历

题目链接429. N 叉树的层序遍历 - 力扣(LeetCode)

题目展示

题目分析

层序遍历即可~仅需多加⼀个变量,用来记录每⼀层结点的个数就好了。

代码实现:

/*
// Definition for a Node.
class Node {
public:int val;vector<Node*> children;Node() {}Node(int _val) {val = _val;}Node(int _val, vector<Node*> _children) {val = _val;children = _children;}
};
*/class Solution {
public:vector<vector<int>> levelOrder(Node* root) {vector<vector<int>> ret;queue<Node*> q;if(root==nullptr){return ret;}q.push(root);while(q.size()){int sz=q.size();//记录当前层节点个数vector<int> tmp;//统计本层节点for(int i=0;i<sz;i++){Node* t=q.front();q.pop();tmp.push_back(t->val);for(Node* child:t->children)//让下一层节点入队{if(child!=nullptr){q.push(child);}}}ret.push_back(tmp);}return ret;}
};

2.  二叉树的锯齿层序遍历

题目链接103. 二叉树的锯齿形层序遍历 - 力扣(LeetCode)

题目展示:

题目分析:

在正常的层序遍历过程中,我们是可以把⼀层的结点放在⼀个数组中去的。 既然我们有这个数组,在合适的层数逆序就可以得到锯齿形层序遍历的结果。

代码实现:

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:vector<vector<int>> zigzagLevelOrder(TreeNode* root) {vector<vector<int>> ret;if(root==nullptr){return ret;}queue<TreeNode*> q;q.push(root);int level=1;while(q.size()){int sz=q.size();vector<int> tmp;for(int i=0;i<sz;i++){auto t=q.front();q.pop();tmp.push_back(t->val);if(t->left) q.push(t->left);if(t->right) q.push(t->right);}//判断是否需要逆序if(level%2==0) reverse(tmp.begin(),tmp.end());ret.push_back(tmp);level++;}return ret;}
};

3. 二叉树最大宽度

题目链接662. 二叉树最大宽度 - 力扣(LeetCode)

题目展示:

题目分析:

代码实现: 

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:int widthOfBinaryTree(TreeNode* root) {vector<pair<TreeNode*,unsigned int>> q;q.push_back({root,1});unsigned int ret=0;while(q.size()){//先更新这一层的宽度auto& [x1,y1]=q[0];auto& [x2,y2]=q.back();ret=max(ret,y2-y1+1);//让下一层进队vector<pair<TreeNode*,unsigned int>> tmp;for(auto& [x,y]:q){if(x->left) tmp.push_back({x->left,y*2});if(x->right) tmp.push_back({x->right,y*2+1});}q=tmp;}return ret;}
};

4. 在每个树行中找最大值

题目链接:515. 在每个树行中找最大值 - 力扣(LeetCode)

题目展示:

题目分析:

代码实现:

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:vector<int> largestValues(TreeNode* root) {vector<int> ret;if(root==nullptr) return ret;queue<TreeNode*> q;q.push(root);while(q.size()){int sz=q.size();int tmp=INT_MIN;for(int i=0;i<sz;i++){auto t=q.front();q.pop();tmp=max(tmp,t->val);if(t->left) q.push(t->left);if(t->right) q.push(t->right);}ret.push_back(tmp);}return ret;}
};

相关文章:

  • 动态规划之二维费用的背包问题解析
  • 数据结构·ST表
  • Git 时光机:修改Commit信息
  • json格式不合法情况下,如何尽量保证数据可用性
  • 音频类网站或者资讯总结
  • 40、C# 数组、链表、哈希、队列、栈数据结构的特点、优点和缺点
  • Python生活手册-NumPy统计:从快递站到咖啡店的数字密码
  • 源码示例:使用SpringBoot+Vue+ElementUI+UniAPP技术组合开发一套小微企业ERP系统
  • Flask 调试的时候进入main函数两次
  • Python教程(四)——数据结构
  • 画立方体软件开发笔记 js three 投影 参数建模 旋转相机 @tarikjabiri/dxf导出dxf
  • 常见音频主控芯片以及相关厂家总结
  • win10-启动django项目时报错
  • Go语言——goflow工作流使用
  • MySQL 中 count(*)、count(1) 和 count(字段名) 有什么区别?
  • 访问者模式(Visitor Pattern)详解
  • excel大表导入数据库
  • RAG 2.0 深入解读
  • OSPF不规则区域划分
  • 从代码学习深度学习 - 语义分割和数据集 PyTorch版
  • 海航回应“男团粉丝为追星堵住机舱通道”:已紧急阻止
  • 外交部发言人就印巴局势升级答记者问
  • 智利观众也喜欢上海的《好东西》
  • 中国金茂新任命三名副总裁,撤销区域公司
  • “毛茸茸”的画,诗意、温暖又治愈
  • 蔡达峰:推动食品安全法全面有效实施,为维护人民群众身体健康提供有力法治保障