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;
    }
};
相关推荐
tankeven14 分钟前
HJ135 计树
c++·算法
㓗冽14 分钟前
时间转换-进阶题12
c++·算法
不知名。。。。。。。。19 分钟前
仿muduo库实现高并发---请求HttpRequest模块 响应HttpResponse模块
服务器·c++
liuyao_xianhui39 分钟前
优选算法_两数之和_位运算_C++
java·开发语言·数据结构·c++·算法·链表·动态规划
博风41 分钟前
算法:双指针解:盛最多水的容器
算法·leetcode
阿Y加油吧1 小时前
力扣打卡day05——找到字符串中所有字母异位词、和为K的子数组
leetcode
也曾看到过繁星1 小时前
类和对象
c++
abant21 小时前
leetcode912 排序算法总结
算法·leetcode·排序算法
liuyao_xianhui1 小时前
优选算法_位运算_只出现一次的数字3_C++
开发语言·数据结构·c++·算法·leetcode·链表·动态规划
十五年专注C++开发2 小时前
Linux 下用 VS Code 高效调试
linux·运维·服务器·c++·vscode