队列+宽搜(bfs)

目录

一:N叉树的层序遍历

1.1题目

1.2算法原理

1.3代码

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

2.1题目

2.2算法原理

2.3代码

三:二叉树最大宽度

3.1题目

3.3算法原理

3.3代码

四:在每个树中找最大值

4.1题目

4.2算法原理

4.3代码


一:N叉树的层序遍历

1.1题目

题目链接:https://leetcode.cn/problems/n-ary-tree-level-order-traversal/


1.2算法原理


1.3代码

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.1题目

题目链接:https://leetcode.cn/problems/binary-tree-zigzag-level-order-traversal/description/


2.2算法原理

锯齿形的层序遍历与第一题的层序遍历唯一不同的是,如果认为头结点是第一层,每一个奇数层都是正常层序遍历的逆序,也就是每次到奇数层的时候,先正常层序遍历,最后push到结果之前逆序即可


2.3代码

cpp 复制代码
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) 
    {
        vector<vector<int>> ret;
        queue<TreeNode*> queue;
        if(root == nullptr) return ret;
        
        queue.push(root);
        int cont = 0;
        while(queue.size())
        {
            int n = queue.size();//当前层要遍历的次数
            vector<int> tmp;//统计当前层的值

            for(int i = 0; i < n;i++)
            {
                TreeNode* cur = queue.front();
                queue.pop();
                tmp.push_back(cur->val);
                if(cur->left)  queue.push(cur->left);
                if(cur->right) queue.push(cur->right);
            }
            if(cont%2) reverse(tmp.begin(),tmp.end());
            ret.push_back(tmp);
            cont++;
        }   

        return ret;
    }
};

三:二叉树最大宽度

3.1题目

题目链接:https://leetcode.cn/problems/maximum-width-of-binary-tree/


3.3算法原理


3.3代码

cpp 复制代码
class Solution {
public:
    int widthOfBinaryTree(TreeNode* root) 
    {
        vector<pair<TreeNode*,unsigned int>> q;//使用数组模拟栈,方便拿去队尾元素
        unsigned int ret = 0;
        q.push_back({root,1});
        while(q.size())
        {
            int n = 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.1题目

题目链接:https://leetcode.cn/problems/find-largest-value-in-each-tree-row/


4.2算法原理

与第一个题目的层序遍历原理相同,只是把每层的值加入数组逻辑改为比较每层值的大小取最大值


4.3代码

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 n = q.size();
            int val = INT_MIN;
            for(int i = 0; i < n;i++)
            {
                TreeNode* cur = q.front();
                q.pop();
                val = max(val,cur->val);
                if(cur->left) q.push(cur->left);
                if(cur->right) q.push(cur->right);
            }
            ret.push_back(val);
        }
        return ret;
    }
};
相关推荐
LabVIEW开发5 分钟前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview
严肃小白兔2 小时前
MATLAB 数值计算中的精度与误差问题:浮点运算、误差传播与数值稳定性
人工智能·算法·matlab
Hhy_11072 小时前
《C++深度解构06》手撕 string —— 从底层内存管理到深浅拷贝彻底吃透
c语言·开发语言·c++·学习·visual studio
青少儿编程课堂2 小时前
蓝桥杯青少组图形化省考报名倒计时·全国青少年信息素养大赛·电子学会图形化考级:校园黑白棋人机对战解析
c++·python·算法·bfs·信息学竞赛
YSL0701242 小时前
C++基础
开发语言·c++
eggwyw3 小时前
关于C++为什么不加入垃圾回收机制解析
开发语言·c++
jktantan3 小时前
注解式脱敏:用 Jackson 的 ContextualSerializer 做字段级策略(一)
spring boot·后端·算法·spring cloud·系统架构
曦樂~13 小时前
Cpp面筋
c语言·c++
道尔柯南13 小时前
C++ string 类详解:从常用接口到深拷贝,彻底掌握字符串处理
android·java·c++
Dream it possible!16 小时前
LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)
算法·leetcode·面试·动态规划