队列+宽搜(bfs)

目录

一:N叉树的层序遍历

1.1题目

1.2算法原理

1.3代码

二:二叉树的锯齿形层序遍历

2.1题目

2.2算法原理

2.3代码

三:二叉树最大宽度

3.1题目

3.3算法原理

3.3代码

四:在每个树中找最大值

4.1题目

4.2算法原理

4.3代码


一:N叉树的层序遍历

1.1题目

题目链接:https://leetcode.cn/problems/n-ary-tree-level-order-traversal/


1.2算法原理


1.3代码

cpp 复制代码
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.1题目

题目链接:https://leetcode.cn/problems/binary-tree-zigzag-level-order-traversal/description/


2.2算法原理

锯齿形的层序遍历与第一题的层序遍历唯一不同的是,如果认为头结点是第一层,每一个奇数层都是正常层序遍历的逆序,也就是每次到奇数层的时候,先正常层序遍历,最后push到结果之前逆序即可


2.3代码

cpp 复制代码
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) 
    {
        vector<vector<int>> ret;
        queue<TreeNode*> queue;
        if(root == nullptr) return ret;
        
        queue.push(root);
        int cont = 0;
        while(queue.size())
        {
            int n = queue.size();//当前层要遍历的次数
            vector<int> tmp;//统计当前层的值

            for(int i = 0; i < n;i++)
            {
                TreeNode* cur = queue.front();
                queue.pop();
                tmp.push_back(cur->val);
                if(cur->left)  queue.push(cur->left);
                if(cur->right) queue.push(cur->right);
            }
            if(cont%2) reverse(tmp.begin(),tmp.end());
            ret.push_back(tmp);
            cont++;
        }   

        return ret;
    }
};

三:二叉树最大宽度

3.1题目

题目链接:https://leetcode.cn/problems/maximum-width-of-binary-tree/


3.3算法原理


3.3代码

cpp 复制代码
class Solution {
public:
    int widthOfBinaryTree(TreeNode* root) 
    {
        vector<pair<TreeNode*,unsigned int>> q;//使用数组模拟栈,方便拿去队尾元素
        unsigned int ret = 0;
        q.push_back({root,1});
        while(q.size())
        {
            int n = 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.1题目

题目链接:https://leetcode.cn/problems/find-largest-value-in-each-tree-row/


4.2算法原理

与第一个题目的层序遍历原理相同,只是把每层的值加入数组逻辑改为比较每层值的大小取最大值


4.3代码

cpp 复制代码
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 n = q.size();
            int val = INT_MIN;
            for(int i = 0; i < n;i++)
            {
                TreeNode* cur = q.front();
                q.pop();
                val = max(val,cur->val);
                if(cur->left) q.push(cur->left);
                if(cur->right) q.push(cur->right);
            }
            ret.push_back(val);
        }
        return ret;
    }
};
相关推荐
rainbow688911 分钟前
Linux文件描述符与重定向原理
c++
独好紫罗兰22 分钟前
对python的再认识-基于数据结构进行-a003-列表-排序
开发语言·数据结构·python
wuhen_n29 分钟前
JavaScript内置数据结构
开发语言·前端·javascript·数据结构
努力学算法的蒟蒻29 分钟前
day79(2.7)——leetcode面试经典150
算法·leetcode·职场和发展
2401_8414956433 分钟前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
AC赳赳老秦35 分钟前
2026国产算力新周期:DeepSeek实战适配英伟达H200,引领大模型训练效率跃升
大数据·前端·人工智能·算法·tidb·memcache·deepseek
CodeSheep程序羊1 小时前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展
独好紫罗兰1 小时前
对python的再认识-基于数据结构进行-a002-列表-列表推导式
开发语言·数据结构·python
2401_841495641 小时前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归
budingxiaomoli1 小时前
优选算法-字符串
算法