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;
    }
};
相关推荐
踩坑记录11 小时前
leetcode hot100 189.轮转数组 medium
leetcode
Dream it possible!12 小时前
LeetCode 面试经典 150_二分查找_在排序数组中查找元素的第一个和最后一个位置(115_34_C++_中等)
c++·leetcode·面试
月光下的麦克13 小时前
如何查案动态库版本
linux·运维·c++
小六子成长记13 小时前
【C++】:搜索二叉树的模拟实现
数据结构·c++·算法
汉克老师13 小时前
GESP2025年9月认证C++二级真题与解析(编程题1(优美的数字))
c++·算法·整除·枚举算法·求余·拆数
carver w14 小时前
MFC入门教程 最简版
c++·mfc
王老师青少年编程14 小时前
信奥赛C++提高组csp-s之倍增算法
c++·csp·信奥赛·csp-s·提高组·倍增算法·rmq
低频电磁之道14 小时前
编译C++的几种方式(MSVC编译器)
开发语言·c++
Zsy_05100314 小时前
【C++】类和对象(一)
开发语言·c++
菜鸟233号14 小时前
力扣377 组合总和 Ⅳ java实现
java·数据结构·算法·leetcode