【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;
    }
};

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

相关推荐
knight_9___34 分钟前
LLM工具调用面试篇6
人工智能·python·面试·职场和发展·llm·agent
承渊政道1 小时前
【动态规划算法】(完全背包问题从状态定义到空间优化)
数据结构·c++·学习·算法·leetcode·动态规划·哈希算法
超级大福宝1 小时前
【力扣48. 旋转图像】超好记忆版 + 口诀
c++·算法·leetcode
爱写代码的倒霉蛋1 小时前
2023年天梯赛L1-8
数据结构·算法
apollowing1 小时前
启发式算法WebApp实验室:从搜索策略到群体智能的能力进阶(上)
算法·启发式算法·web app
生物信息与育种2 小时前
黄三文院士领衔植物星球计划(PLANeT)发表Cell
人工智能·深度学习·算法·面试·transformer
aini_lovee2 小时前
WSN 四大经典无需测距定位算法
算法
人道领域2 小时前
【LeetCode刷题日记】掌握二叉树遍历:栈实现的三种绝妙方法
算法·leetcode·职场和发展
北冥湖畔的燕雀2 小时前
深入解析Linux信号处理机制
算法
阿Y加油吧2 小时前
二刷 LeetCode:动态规划经典双题复盘
算法·leetcode·动态规划