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;
    }
};
相关推荐
0 0 03 小时前
CCF-CSP 39-2 水印检查(watermark)【C++】
c++·算法
plus4s4 小时前
2月15日(78,80,81题)
c++·算法·图论
期末考复习中,蓝桥杯都没时间学了6 小时前
力扣刷题19
算法·leetcode·职场和发展
zmzb01037 小时前
C++课后习题训练记录Day104
开发语言·c++
honiiiiii7 小时前
SMU winter week4
c++
踩坑记录7 小时前
递归回溯本质
leetcode
zmzb01037 小时前
C++课后习题训练记录Day105
开发语言·c++·算法
闻缺陷则喜何志丹8 小时前
【拆位法】P8743 [蓝桥杯 2021 省 A] 异或数列|普及+
c++·蓝桥杯·位运算·拆位法
好学且牛逼的马8 小时前
【Hot100|25-LeetCode 142. 环形链表 II - 完整解法详解】
算法·leetcode·链表
fpcc8 小时前
跟我学C++中级篇——Concepts的循环依赖
c++·模板和元编程