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;
    }
};
相关推荐
kyle~18 小时前
C++---value_type 解决泛型编程中的类型信息获取问题
java·开发语言·c++
NiNi_suanfa21 小时前
【Qt】Qt 批量修改同类对象
开发语言·c++·qt
信奥胡老师21 小时前
苹果电脑(mac系统)安装vscode与配置c++环境,并可以使用万能头文件全流程
c++·ide·vscode·macos·编辑器
妖灵翎幺21 小时前
C++ 中的 :: 操作符详解(一切情况)
开发语言·c++·ide
star _chen1 天前
C++实现完美洗牌算法
开发语言·c++·算法
繁星星繁1 天前
【C++】脚手架学习笔记 gflags与 gtest
c++·笔记·学习
路痴楷1 天前
无法定位程序输入点问题
c++·qt·visual studio
Source.Liu1 天前
【LibreCAD】 RS_Units 类完整解析
c++·qt·rust
我是一棵无人问荆的小草1 天前
编码演变史
开发语言·c++
potato_may1 天前
CC++ 内存管理 —— 程序的“五脏六腑”在哪里?
c语言·开发语言·数据结构·c++·内存·内存管理