LeCode:(102. 二叉树的层序遍历;107. 二叉树的层序遍历 II)

题目1

题目链接

本题与层序遍历不同的是,是一层一层的输出。

难点:如何一层一层的输出(需要知道每层的个数)

解题思路:
第一层只有一个结点,我们可以使用count计数,记录每层有几个结点,记录第二层有几个结点。然后根据第二层count计数,记录第三层有几个结点。直到遍历完。

cpp 复制代码
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        //根入,遍历,count++
        vector<vector<int>> outPut;  //输出的总数组
        vector<int> t;    //每层的小数组
        queue<TreeNode*> cur;   //队列用来记录层序遍历的结点
        int count = 1; //记录本层个数,第一层为1
        if(root == nullptr)  //树为空,直接返回大数组
        {
            return outPut;
        }
        cur.push(root);   //先将根入队
        while(!cur.empty())  //队列为空,遍历完毕
        {
            int k = count;   //把本层个数给k,
            count = 0;       //count清0, 接着记录下一层
            while(k--)
            {
                TreeNode* tem = cur.front();  
                cur.pop();        
                t.push_back(tem->val);  //将值给小数组
                if(tem->left)
                {
                    count++;
                    cur.push(tem->left);
                } 
                if(tem->right)
                {
                    count++;
                    cur.push(tem->right);
                }
            }
            outPut.push_back(t);   //一层遍历完
            t.clear();  //小数组清空
        }
        return outPut;
    }
};

题目2

题目链接

以为会有新思路,结果官方答案把题目1,最后输出数组翻转了一下。

cpp 复制代码
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {

        //根入,遍历,count++
        vector<vector<int>> outPut;  //输出的总数组
        vector<int> t;    //每层的小数组
        queue<TreeNode*> cur;   //队列用来记录层序遍历的结点
        int count = 1; //记录本层个数,第一层为1
        if(root == nullptr)  //树为空,直接返回大数组
        {
            return outPut;
        }
        cur.push(root);   //先将根入队
        while(!cur.empty())  //队列为空,遍历完毕
        {
            int k = count;   //把本层个数给k,
            count = 0;       //count清0, 接着记录下一层
            while(k--)
            {
                TreeNode* tem = cur.front();  
                cur.pop();        
                t.push_back(tem->val);  //将值给小数组
                if(tem->left)
                {
                    count++;
                    cur.push(tem->left);
                } 
                if(tem->right)
                {
                    count++;
                    cur.push(tem->right);
                }
            }
            outPut.push_back(t);   //一层遍历完
            t.clear();  //小数组清空
        }
        reverse(outPut.begin(),outPut.end());
        return outPut;
    }
};
相关推荐
CoovallyAIHub1 天前
港大&字节重磅发布DanceGRPO:突破视觉生成RLHF瓶颈,多项任务性能提升超180%!
深度学习·算法·计算机视觉
感哥1 天前
C++ STL 常用算法
c++
CoovallyAIHub1 天前
英伟达ViPE重磅发布!解决3D感知难题,SLAM+深度学习完美融合(附带数据集下载地址)
深度学习·算法·计算机视觉
saltymilk1 天前
C++ 模板参数推导问题小记(模板类的模板构造函数)
c++·模板元编程
感哥1 天前
C++ lambda 匿名函数
c++
沐怡旸2 天前
【底层机制】std::unique_ptr 解决的痛点?是什么?如何实现?怎么正确使用?
c++·面试
感哥2 天前
C++ 内存管理
c++
聚客AI2 天前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v2 天前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工2 天前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法