C++ | Leetcode C++题解之第111题二叉树的最小深度

题目:

题解:

cpp 复制代码
class Solution {
public:
    int minDepth(TreeNode *root) {
        if (root == nullptr) {
            return 0;
        }

        queue<pair<TreeNode *, int> > que;
        que.emplace(root, 1);
        while (!que.empty()) {
            TreeNode *node = que.front().first;
            int depth = que.front().second;
            que.pop();
            if (node->left == nullptr && node->right == nullptr) {
                return depth;
            }
            if (node->left != nullptr) {
                que.emplace(node->left, depth + 1);
            }
            if (node->right != nullptr) {
                que.emplace(node->right, depth + 1);
            }
        }

        return 0;
    }
};
相关推荐
AA陈超5 分钟前
虚幻引擎5 GAS开发俯视角RPG游戏 P07-08 点击移动
c++·游戏·ue5·游戏引擎·虚幻
小白程序员成长日记28 分钟前
2025.11.07 力扣每日一题
数据结构·算法·leetcode
·白小白30 分钟前
力扣(LeetCode) ——209. 长度最小的子数组(C++)
c++·算法·leetcode
ohnoooo934 分钟前
251106 算法
数据结构·c++·算法
卡提西亚1 小时前
C++笔记-24-文件读写操作
开发语言·c++·笔记
m0_748248021 小时前
C++ 异常处理全解析:从语法到设计哲学
java·c++·word
小白程序员成长日记1 小时前
2025.11.08 力扣每日一题
算法·leetcode·职场和发展
m0_748248023 小时前
C++20 协程:在 AI 推理引擎中的深度应用
java·c++·人工智能·c++20
他们叫我一代大侠3 小时前
Leetcode :模拟足球赛小组各种比分的出线状况
算法·leetcode·职场和发展
QT 小鲜肉3 小时前
【Git、GitHub、Gitee】按功能分类汇总Git常用命令详解(超详细)
c语言·网络·c++·git·qt·gitee·github