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;
    }
};
相关推荐
小白菜又菜1 小时前
Leetcode 2075. Decode the Slanted Ciphertext
算法·leetcode·职场和发展
史迪仔01122 小时前
[QML] QML IMage图像处理
开发语言·前端·javascript·c++·qt
会编程的土豆3 小时前
【数据结构与算法】再次全面了解LCS底层
开发语言·数据结构·c++·算法
低频电磁之道3 小时前
解决 Windows C++ DLL 导出类不可见的编译错误
c++·windows
君义_noip5 小时前
信息学奥赛一本通 4150:【GESP2509七级】⾦币收集 | 洛谷 P14078 [GESP202509 七级] 金币收集
c++·算法·gesp·信息学奥赛·csp-s
Ricky_Theseus5 小时前
静态链接与动态链接
c++
摸个小yu5 小时前
【力扣LeetCode热题h100】链表、二叉树
算法·leetcode·链表
澈2075 小时前
双指针,数组去重
c++·算法
小辉同志5 小时前
207. 课程表
c++·算法·力扣·图论
feng_you_ying_li6 小时前
C++11,{}的初始化情况与左右值及其引用
开发语言·数据结构·c++