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;
    }
};
相关推荐
To_OC20 小时前
LC 51 N 皇后:我以为难的是回溯,结果栽在了对角线下标
javascript·算法·leetcode
2401_8414956421 小时前
【操作系统】进程同步与互斥实验报告
c++·算法·操作系统·进程·并发·同步·互斥
fqbqrr21 小时前
2607C++,soui与安卓
c++·soui
fqbqrr1 天前
2607C++,使用微软detours勾挂工具
c++
蓝悦无人机1 天前
C++基础 — 函数总结
开发语言·c++
我不是懒洋洋1 天前
从零实现一个分布式监控:Prometheus的核心设计
c++
An_s1 天前
c++对接pdfium(一)win系统篇
开发语言·c++
众少成多积小致巨1 天前
C++ 规范参考(上)
c++
闪电悠米1 天前
力扣hot100-73.矩阵置零-标记数组详解
算法·leetcode·矩阵
一拳一个呆瓜1 天前
【STL】iostream 编程:使用提取运算符
c++·stl