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;
    }
};
相关推荐
ShineWinsu4 小时前
对于C++:IO流状态的详细解析
c++·面试·io·cin·io流状态·goodbit·failbit
数模竞赛Paid answer4 小时前
2026年中青杯数学建模A题数学建模论文智能评估系统与多智能体优化方法求解全过程论文及程序
算法·数学建模·中青杯
银-豆豆5 小时前
数据结构与算法-动态规划、回溯与贪心
算法·动态规划
Lzg_na5 小时前
constexpr的优势
c++
想吃火锅10056 小时前
【leetcode】200. 岛屿数量
算法·leetcode·职场和发展
王维同学6 小时前
进程模块枚举、映像身份与线程启动地址关联
c++·windows·安全
hold?fish:palm7 小时前
24 回文链表
数据结构·c++·链表
举手7 小时前
Dispatcher模块剖析
linux·c++
Nil2087 小时前
leetcode 54螺旋矩阵
算法·leetcode·矩阵
依然鸣8 小时前
PTA团体程序设计天梯赛L1真题讲解L1-077-080
开发语言·c++·算法·深度优先·pat考试·图论