前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >【算法刷题指南】队列+宽搜(BFS)

【算法刷题指南】队列+宽搜(BFS)

作者头像
南桥
发布2024-11-27 08:38:21
发布2024-11-27 08:38:21
5200
代码可运行
举报
文章被收录于专栏:南桥谈编程南桥谈编程
运行总次数:0
代码可运行

429.N叉树的层序遍历

代码语言:javascript
代码运行次数:0
复制
/*
// 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>> ans;
        queue<Node*> q;
        q.push(root);
        if(root==nullptr) return ans;
        while(q.size())
        {
            int sz=q.size();
            vector<int> tmp;
            while(sz--)
            {
                Node* t=q.front();
                q.pop();
                tmp.push_back(t->val);
                for(Node* child:t->children)
                {
                    if(child!=nullptr) q.push(child);
                }
            }
            ans.push_back(tmp);
        }
        return ans;
    }
};

103.二叉树的锯齿形层序遍历

代码语言:javascript
代码运行次数:0
复制
/**
 * 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>> ans;
        if(root==nullptr) return ans;

        queue<TreeNode*> q;
        q.push(root);
        int lev=1;

        while(q.size())
        {
            int sz=q.size();
            vector<int> tmp;
            while(sz--)
            {
                TreeNode* 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(lev%2==0) reverse(tmp.begin(),tmp.end());
            ans.push_back(tmp);
            lev++;
        }
        return ans;
    }
};

662.二叉树最大宽度

代码语言:javascript
代码运行次数:0
复制
/**
 * 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 ans = 0;
        while (q.size()) {
            auto& [x1, y1] = q[0];
            auto& [x2, y2] = q.back();
            ans = max(ans, 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 ans;
    }
};

515.在每个树行中找最大值

代码语言:javascript
代码运行次数:0
复制
/**
 * 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> ans;
        if(root==nullptr) return ans;
        queue<TreeNode*> q;
        q.push(root);

        while(q.size())
        {
            int sz=q.size();
            int _max=INT_MIN;
            while(sz--)
            {
                auto t=q.front();
                q.pop();

                _max=max(_max,t->val);
                if(t->left) q.push(t->left);
                if(t->right) q.push(t->right);

            }
            ans.push_back(_max);
        }
        return ans;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-11-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 429.N叉树的层序遍历
  • 103.二叉树的锯齿形层序遍历
  • 662.二叉树最大宽度
  • 515.在每个树行中找最大值
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档