LeetCode 刷题 -- Day 6

今日题目

题目 难度 备注
102. 二叉树的层序遍历 中等 一招鲜吃遍天
107. 二叉树的层序遍历 II ) 中等
199. 二叉树的右视图 中等
637. 二叉树的层平均值 简单
429. N 叉树的层序遍历 中等
515. 在每个树行中找最大值 中等
116. 填充每个节点的下一个右侧节点指针 中等
104. 二叉树的最大深度 简单
111. 二叉树的最小深度 简单

树篇Ⅰ -- 层次遍历

题目:102. 二叉树的层序遍历

102. 二叉树的层序遍历 - 力扣(LeetCode)

一、源代码

c++ 复制代码
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int> > ans;
        if(root == nullptr){
            return ans;
        }
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {                                    //层次遍历队列
            int n = q.size();
            vector<int> layer;
            while(n--) {                                       // 遍历每一层,将遍历的元素出队,并将下一层压入队列
                TreeNode* now = q.front();
                q.pop();
                layer.push_back(now->val);                     //存储每一层的结点值
                if (now->left) q.push(now->left);
                if (now->right) q.push(now->right);
            }
            ans.push_back(layer);
            layer.clear();
        }
        return ans;
    }
};

二、代码思路

利用 queue<TreeNode*> q 实现树的层次遍历。在遍历队列的过程中,利用while(q.size()--) 实现遍历每一层,将遍历的元素出队,并将下一层压入队列,最后就得到了各层结点值了。


题目:107. 二叉树的层序遍历 II

107. 二叉树的层序遍历 II - 力扣(LeetCode)

一、源代码

c++ 复制代码
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> ans;
        if (root == nullptr) {
            return ans;
        }
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {                        // 层次遍历队列
            int n = q.size();
            vector<int> layer;
            while (n--) {                           // 遍历每一层,将遍历的元素出队,并将下一层压入队列
                TreeNode* now = q.front();
                q.pop();
                layer.push_back(now->val);          // 存储每一层的结点值
                if (now->left)
                    q.push(now->left);
                if (now->right)
                    q.push(now->right);
            }
            ans.push_back(layer);
            layer.clear();
        }
        reverse(ans.begin(),ans.end());             //反转层次遍历,得到自底向上的层次遍历
        return ans;
    }
};

二、代码思路

反转层次遍历,得到自底向上的层次遍历。官方也这样做,那就心安理得,直接下一题了咯。


题目:199. 二叉树的右视图

199. 二叉树的右视图 - 力扣(LeetCode)

一、源代码

c++ 复制代码
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        vector<int> ans;
        if (root == nullptr) {
            return ans;
        }
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {                                 // 层次遍历队列
            int n = q.size();
            while (n--) {                                    // 遍历每一层,将遍历的元素出队,并将下一层压入队列
                TreeNode* now = q.front();
                q.pop();
                if(n == 0)  ans.push_back(now->val);         // 将每层最后一个结点压入ans数组中
                if (now->left)  q.push(now->left);
                if (now->right) q.push(now->right);
            }
        }
        return ans;
    }
};

二、代码思路

层次遍历队列,将每层最后一个结点压入ans数组中(此时 n == 0)。


题目:637. 二叉树的层平均值

637. 二叉树的层平均值 - 力扣(LeetCode)

一、源代码

c++ 复制代码
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        vector<double> ans;
        if (root == nullptr) {
            return ans;
        }
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {                                  // 层次遍历队列
            int cnt = q.size(), n = cnt;
            double sum = 0;
            while (cnt--) {                                   // 遍历每一层,将遍历的元素出队,并将下一层压入队列
                TreeNode* now = q.front();
                q.pop();
                sum += now->val;                              // 统计每层结点值之和
                if (now->left)  q.push(now->left);
                if (now->right) q.push(now->right);
            }
            ans.push_back(sum / n);                           // 计算平均值并存入 ans 数组
        }
        return ans;
    }
};

二、代码思路

层次遍历队列,统计每层结点值之和,最后计算平均值并存入 ans数组。


题目:429. N 叉树的层序遍历

429. N 叉树的层序遍历 - 力扣(LeetCode)

一、源代码

c++ 复制代码
class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        vector<vector<int>> ans;
        if (root == nullptr) {
            return ans;
        }
        queue<Node*> q;
        q.push(root);
        while (!q.empty()) {                           // 层次遍历队列
            int n = q.size();
            vector<int> layer;
            while (n--) {                             // 遍历每一层,将遍历的元素出队,并将下一层压入队列
                Node* now = q.front();
                q.pop();
                layer.push_back(now->val);            // 存储每一层的结点值
                for (int i = 0; i < now->children.size(); i++) {
                    q.push(now->children[i]);
                }
            }
            ans.push_back(layer);
            layer.clear();
        }
        return ans;
    }
};

二、代码思路

利用 queue<Node*> q 实现树的层次遍历。在遍历队列的过程中,利用while(q.size()--) 实现遍历每一层,将遍历的元素出队,并将下一层压入队列,最后就得到了各层结点值了。


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

515. 在每个树行中找最大值 - 力扣(LeetCode)

一、源代码

c++ 复制代码
class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        vector<int> ans;
        if (root == nullptr) {
            return ans;
        }
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {                       // 层次遍历队列
            int cnt = q.size(), n = cnt;
            int maxVal = INT_MIN;
            while (cnt--) {                      // 遍历每一层,将遍历的元素出队,并将下一层压入队列
                TreeNode* now = q.front();
                q.pop();
                if (now->val > maxVal) maxVal = now->val;
                if (now->left)  q.push(now->left);
                if (now->right) q.push(now->right);
            }
            ans.push_back(maxVal);
        }
        return ans;
    }
};

题目:116. 填充每个节点的下一个右侧节点指针

116. 填充每个节点的下一个右侧节点指针 - 力扣(LeetCode)

一、源代码

c++ 复制代码
class Solution {
public:
    Node* connect(Node* root) {
        if (root == nullptr) {
            return root;
        }
        queue<Node*> q;
        q.push(root);
        while (!q.empty()) {               // 层次遍历队列
            int cnt = q.size();
            while (cnt--) {                // 遍历每一层,将遍历的元素出队,并将下一层压入队列
                Node* now = q.front();
                q.pop();
                if (cnt > 0) {             // 连接
                    now->next = q.front();
                }
                if (now->left)
                    q.push(now->left);
                if (now->right)
                    q.push(now->right);
            }
        }
        return root;
    }
};

二、代码思路

初始状态下,所有 next 指针都被设置为 NULL。所以只要层次遍历树,在每层中进行连接就行。


题目:104. 二叉树的最大深度

104. 二叉树的最大深度 - 力扣(LeetCode)

一、源代码

c++ 复制代码
class Solution {
private:
    int DFS(TreeNode* root,int h) {
        if (!root) return h;
        int l = DFS(root->left,h+1);
        int r = DFS(root->right,h+1);
        return l > r ? l : r;
    }
public:
    int maxDepth(TreeNode* root) {
        return DFS(root,0);
    }
};

二、代码思路

利用递归,每次递归处理一层中的一个结点。对每一层的一个结点有两种情况:

① root 为空指针,则说明递归到底,返回 高度h 就行。

② root 不为空,则找它的左右孩子的高度,并返回较大的高度 h。

对树顶点开始执行递归就得到了最大高度。


题目:111. 二叉树的最小深度

111. 二叉树的最小深度 - 力扣(LeetCode)

一、源代码

c++ 复制代码
class Solution {
private:
    int minH = INT_MAX;
    void DFS(TreeNode* root, int h) {
        if (!root)
            return ;
        if (!root->left && !root->right) {        // 遇到叶子结点则更新 minH
            minH = min(minH,h+1);
        }
        DFS(root->left,h+1);
        DFS(root->right,h+1);
    }

public:
    int minDepth(TreeNode* root) {
        DFS(root,0);
        return root ? minH : 0;                  // 为空指针返回 0,否则返回 minH
    }
};

二、代码思路

DFS 遍历树,且每下一层高度 h+1,当访问到叶子结点时,就得出了一条从根节点到最近叶子结点的路径长度了(为当前h+1),记录最小的路径长度即为答案

相关推荐
国科安芯14 分钟前
FreeRTOS RISC-V 浮点上下文切换移植:在 IAR 工程中完整保存 FPU 寄存器
java·开发语言·单片机·嵌入式硬件·算法·系统架构·risc-v
小陈phd37 分钟前
集成检索介绍
算法
小poop39 分钟前
轮转数组:从暴力到最优,一题掌握算法复杂度分析
数据结构·算法·leetcode
笨鸟先飞的橘猫1 小时前
只会脚本语言的游戏后端自我提升之路——c++学习(开篇)
学习·游戏
樱桃读报僵尸1 小时前
说说 LLMRouter,Agent 执行过程中怎么动态的选择 LLM
算法
gis开发之家2 小时前
《Vue3 从入门到大神40篇》Vue3 源码详解(十):diff 算法全解析 —— 为什么 Vue3 比 Vue2 更快?
javascript·算法·typescript·前端框架·vue3·vue3源码
进击的丸子2 小时前
虹软人脸服务器SDK-C++语言Demo实操指南
后端·算法
兰令水2 小时前
hot100【acm版】【2026.7.25/26打卡-java版本】
java·算法·排序算法
quanjui3 小时前
【医学尝试】基于Segment Anything Model的医学图像分割研究:眼底OCT与X线胸片微调实战
人工智能·笔记·学习
YUS云生3 小时前
大模型学习·第40天:LangChain框架入门——从RAG原理到模型调用的统一接口
学习·langchain