队列

队列

晚风吻尽荷花叶 6 2025-03-10

一.宽搜

1.515. 在每个树行中找最大值 - 力扣(LeetCode)

429. N 叉树的层序遍历 - 力扣(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 n = q.size();
            int num = INT_MIN;
            while(n--)
            {
                TreeNode *t = q.front();
                q.pop();
                num = max(num,t->val);
                if(t->left)
                    q.push(t->left);
                if(t->right)
                    q.push(t->right);
            }
            ret.push_back(num);
        }
        return ret;
    }
};

2.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;
        if(root == nullptr)
            return ret;

        queue<Node*> q;
        q.push(root);
        while(!q.empty())
        {
            int n = q.size();
            vector<int> tmp;
            for(int i = 0;i < n;i++)
            {
                Node* t = q.front();
                q.pop();
                tmp.push_back(t->val);
                for(auto e : t->children)
                {
                    if(e)
                        q.push(e);
                }
            }
            ret.push_back(tmp);
        }
        return ret;
    }
};

3.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 flag = 0;
        while(q.size())
        {
            int n = q.size();
            vector<int> v;
            while(n--)
            {
                TreeNode *t = q.front();
                q.pop();
                v.push_back(t->val);
                if(t->left)
                    q.push(t->left);
                if(t->right)
                    q.push(t->right);
            }
            flag++;
            if(flag % 2 == 0)
                reverse(v.begin(),v.end());
            ret.push_back(v);
        }
        return ret;
    }
};

4.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,2 * y});
                if(x->right)
                    tmp.push_back({x->right,2 * y + 1});
            }
            q = tmp;
        }
        return ret;
    }
};