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;
    }
};
相关推荐
风中的微尘2 小时前
39.网络流入门
开发语言·网络·c++·算法
混分巨兽龙某某3 小时前
基于Qt Creator的Serial Port串口调试助手项目(代码开源)
c++·qt creator·串口助手·serial port
小冯记录编程3 小时前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio
C_Liu_4 小时前
C++:类和对象(下)
开发语言·c++
coderxiaohan4 小时前
【C++】类和对象1
java·开发语言·c++
阿昭L4 小时前
MFC仿真
c++·mfc
老赵的博客7 小时前
c++ unqiue指针
java·jvm·c++
程序猿编码7 小时前
基于 Linux 内核模块的字符设备 FIFO 驱动设计与实现解析(C/C++代码实现)
linux·c语言·c++·内核模块·fifo·字符设备
怎么没有名字注册了啊8 小时前
MFC_Install_Create
c++·mfc
Wadli8 小时前
C++语法 | static静态|单例模式
开发语言·c++·单例模式