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;
    }
};
相关推荐
888CC++1 小时前
C语言与C++的区别:从面向过程到面向对象
java·c语言·c++
Darkwanderor3 小时前
Linux系统编程实战项目:模拟实现shell
linux·c++
himobrinehacken3 小时前
揭秘Windows程序启动的神秘之旅
c++·安全
库玛西4 小时前
C++ 运行时多态 :核心总结与原理图解
c语言·c++·笔记
Byron Loong5 小时前
【C++】重定向是什么
开发语言·c++
hansang_IR5 小时前
【题解】LC:Z 算法(Z Algorithm)
c++·算法·字符串
橘子汽水1685 小时前
Leetcode 230,98:二叉搜索树中第K小的元素,验证二叉搜索树
算法·leetcode·职场和发展
霍霍的袁7 小时前
【C++】模板从入门到进阶(函数模板 + 类模板 + 特化 + 分离编译)
开发语言·c++·visual studio
海清河晏1117 小时前
Qt 实战:信号与槽+事件系统
开发语言·c++·qt
hansang_IR7 小时前
【题解】LC:卷积(Convolution (Mod 1,000,000,007))
c++·算法·ntt·crt