【LeetCode热题100】队列+宽搜

这篇博客是关于队列+宽搜的几道题,主要包括N叉树的层序遍历、二叉树的锯齿形层序遍历、二叉树最大宽度、在每个数行中找最大值。

cpp 复制代码
class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) 
    {
        vector<vector<int>> ret;
        if(!root) return ret;
        queue<Node*> q;

        q.push(root);
        while(q.size())
        {
            int num = q.size();  //先求出本层的元素个数
            vector<int> tmp;  //统计本层的节点
            while(num--)
            {
                Node* top = q.front();
                q.pop();
                tmp.push_back(top->val);
                for(auto e : top->children)
                {
                    if(e != nullptr)
                        q.push(e);
                }
            }
            ret.push_back(tmp);
        }
        return ret;
        
    }
};

题目分析:这道题我们需要层序遍历,需要借助一个队列实现,首先将第一层节点放进队列,然后出队列,在出队列后,把它的孩子节点都push到队列中,再依次把这几个孩子节点出队列,每一个节点出队列后,都要马上把它的孩子节点push到队列。为了知道每层有几个节点,在每一层出队列前,需要统计队列里的元素个数。

cpp 复制代码
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) 
    {
        vector<vector<int>> ret;
        if(!root) return ret;
        queue<TreeNode*> q;
        q.push(root);
        int flag = 1;
        while(q.size())
        {
            int sz = q.size();
            vector<int> tmp;
            for(int i = 0 ; i < sz ; i++)
            {
                TreeNode* top = q.front();
                q.pop();
                tmp.push_back(top->val);
                if(top->left) q.push(top->left);
                if(top->right) q.push(top->right);
            }
            if(flag % 2 == 0)
            {
                reverse(tmp.begin(), tmp.end());
            }
            ret.push_back(tmp);
            flag++;
        }
        return ret;
    }
};

题目分析:仍然是使用队列来存放节点,和上一题不同的是,在得到偶数层的队列后,需要将其逆序一下,可以通过创建一个变量来判断奇偶层。

cpp 复制代码
class Solution {
public:
    int widthOfBinaryTree(TreeNode* root) 
    {
        vector<pair<TreeNode*,unsigned int>> queue;
        unsigned int ret = 0;
        queue.push_back({root, 1});
        while(queue.size())
        {
            auto& [x1, y1] = queue[0];
            auto& [x2, y2] = queue.back();
            ret = max(ret, y2 - y1 + 1);

            vector<pair<TreeNode*,unsigned int>> tmp;
            for(auto& [x, y] : queue)
            {
                if(x->left) tmp.push_back({x->left, 2*y});
                if(x->right) tmp.push_back({x->right, 2*y + 1}); 
            }
            queue = tmp;
        } 
        return ret;
    }
};

题目分析:这道题我们可以使用数组存储二叉树的方式,给节点编号,数组的类型为pair<TreeNode*,int>,int为这个节点的编号,一层的两端节点编号相减+1就是这层的宽度。

需要注意的是,下标可能溢出,所以不能用int存储节点编号,而是用unsigned int 存储。

cpp 复制代码
class Solution {
public:
    vector<int> largestValues(TreeNode* root) 
    {
        queue<TreeNode*> q;
        vector<int> ret;
        if(!root) return ret;
        q.push(root);
        // ret.push_back(root->val);
        while(q.size())
        {
            int size = q.size();
            int m = INT_MIN;
            while(size--)
            {
                TreeNode* top = q.front();
                q.pop();
                m = max(m, top->val);
                if(top->left) q.push(top->left);
                if(top->right) q.push(top->right);
            }
            ret.push_back(m);
        }
        return ret;
    }
};

题目分析:很简单,利用层序遍历,统计每一层的最大值。

相关推荐
稚南城才子,乌衣巷风流5 分钟前
支配树(Dominator Tree)详解:概念、算法与应用
算法
稚南城才子,乌衣巷风流34 分钟前
动态开点:原理、实现与应用场景
数据结构·算法
yyds_yyd_100861 小时前
1464. 数组中两元素的最大乘积(2026.07.27)
数据结构·c++·算法·leetcode
KaMeidebaby2 小时前
卡梅德生物技术快报 | 核酸适配体文库测序:核酸适配体文库测序的技术原理、实验流程与数据解析
前端·网络·数据库·人工智能·算法
geovindu2 小时前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
网络安全零基础教程3 小时前
网络安全春招面试避坑与复盘指南
linux·网络·安全·web安全·面试·职场和发展
ekkoalex4 小时前
训练框架以及算法实现的框架
算法
DFT计算杂谈4 小时前
交错磁研究进展材料物性与交叉应用
数据库·人工智能·python·opencv·算法
玖玥拾4 小时前
LeetCode 26 删除有序数组中的重复项
算法·leetcode
Kina_C4 小时前
LVS负载均衡的十二种核心调度算法
linux·运维·算法·负载均衡·lvs