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

题目1

题目链接

在这里插入图片描述
本题与层序遍历不同的是,是一层一层的输出。
难点:如何一层一层的输出(需要知道每层的个数)

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

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,最后输出数组翻转了一下。

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;
    }
};

相关推荐

  1. 107. II

    2024-01-12 23:52:05       35 阅读
  2. 102.

    2024-01-12 23:52:05       31 阅读
  3. 102.

    2024-01-12 23:52:05       30 阅读
  4. leetcode 102.

    2024-01-12 23:52:05       29 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-12 23:52:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-12 23:52:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-12 23:52:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-12 23:52:05       20 阅读

热门阅读

  1. 【力扣每日一题】力扣447回旋镖的数量

    2024-01-12 23:52:05       37 阅读
  2. UR5机械臂控制

    2024-01-12 23:52:05       38 阅读
  3. tcp 的四次挥手

    2024-01-12 23:52:05       35 阅读
  4. tcp的三次握手

    2024-01-12 23:52:05       33 阅读
  5. 1.5如何用命令得到自己的ip<本地>

    2024-01-12 23:52:05       38 阅读
  6. 1、HarmonyOS简介

    2024-01-12 23:52:05       33 阅读
  7. 6.停车场管理系统

    2024-01-12 23:52:05       26 阅读
  8. DEJA_VU3D - Cesium功能集 之 113-获取圆节点(2)

    2024-01-12 23:52:05       41 阅读