优选算法专题12:队列

队列

目录

队列

试题1:N叉树的层序遍历算法解析

算法原理

代码编写

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

算法原理

代码编写

试题3:二叉树的最大宽度

算法原理

代码编写

试题4:在每个树行中找最大值

算法原理

代码编写


试题1:N叉树的层序遍历算法解析

算法原理

解法:宽度优先搜索

代码编写

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:二叉树的锯齿形层序遍历

算法原理

解法:层序遍历

增加一个标记位,让偶数行信息逆序

代码编写

cpp 复制代码
class Solution 
{
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) 
    {
        vector<vector<int>> ret;
        queue<TreeNode*> q;

       if(root ==  nullptr)
       {
            return ret;
       } 

       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:二叉树的最大宽度

算法原理

解法一:层序遍历(超时)

解法二:利用数组存储二叉树(堆)优化

相减之后即使溢出结果也是正确的

代码编写

cpp 复制代码
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:在每个树行中找最大值

算法原理

解法:层序遍历

统计出每一层最大值

代码编写

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 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;
    }
};
相关推荐
ting945200018 小时前
Humalike X Hermes 深度技术剖析:单指令注入群聊社交智能的底层架构、算法与跨 IM 平台实现
人工智能·算法·架构
吴声子夜歌19 小时前
Java面试——算法
java·算法·面试
evans在进步19 小时前
LeetCode 64:最小路径和——Java 原地动态规划详解
java·leetcode·动态规划
h_a_o777oah19 小时前
【图论】Tarjan 缩点:解决有向图中环的问题
c++·算法·图论·acm·强连通分量·缩点·tarjan
Tisfy19 小时前
LeetCode 1386.安排电影院座位:哈希表+位运算
算法·leetcode·散列表·题解·哈希表
Nil20820 小时前
leetcode 199二叉树的右视图
算法·leetcode·深度优先
M78佐菲21 小时前
c语言学习笔记:排序与查找方法整理
linux·c语言·笔记·学习·算法
AI备案指南-满满1 天前
人工智能拟人化互动服务安全自评估报告的评估要点有哪些?
人工智能·算法·安全·机器人·大模型备案·算法备案
土司大王1 天前
LeetCode hot100——两两交换链表中的节点
算法·leetcode·职场和发展