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;
    }
};
相关推荐
lvwangshu6 小时前
CF1882D Tree XOR 题解
动态规划·题解·贪心·dp·树形dp·换根dp·性质题·思维好题·推导题
文丑颜不良啊7 小时前
LeetCode201~225题目解析
java·leetcode
努力实干7 小时前
面向对象设计原则——C++
开发语言·c++·数码相机
syagain_zsx10 小时前
jsoncpp 使用指南:编译链接、Json::Value 取值与高频坑
c++·json·库·http格式
Nil20810 小时前
LeetCode 283移动零
数据结构·算法·leetcode
布莱克60511 小时前
理解 = delete 与 = default:两种赋值语义的差异
c++
文丑颜不良啊11 小时前
LeetCode226~250题目解析
java·leetcode
小小、码农12 小时前
[特殊字符]【C++11】类的新功能全面解析:移动构造、移动赋值、default、delete、final 与 override
linux·开发语言·网络·c++
库玛西12 小时前
外部排序全解析:败者树、置换-选择排序与最佳归并树
c语言·数据结构·c++·笔记·考研·算法
General_G14 小时前
创建型模式—原型(Prototype)模式
c++·设计模式·原型模式