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;
    }
};
相关推荐
Larry_Yanan2 小时前
QML学习笔记(四十)QML的ApplicationWindow和StackView
c++·笔记·qt·学习·ui
Miraitowa_cheems3 小时前
LeetCode算法日记 - Day 73: 最小路径和、地下城游戏
数据结构·算法·leetcode·职场和发展·深度优先·动态规划·推荐算法
野蛮人6号3 小时前
力扣热题100道之560和位K的子数组
数据结构·算法·leetcode
Swift社区4 小时前
LeetCode 400 - 第 N 位数字
算法·leetcode·职场和发展
Kratzdisteln5 小时前
【C语言】Dev-C++如何编译C语言程序?从安装到运行一步到位
c语言·c++
剪一朵云爱着5 小时前
力扣2080. 区间内查询数字的频率
算法·leetcode
Dream it possible!6 小时前
LeetCode 面试经典 150_栈_有效的括号(52_20_C++_简单)(栈+哈希表)
c++·leetcode·面试··哈希表
kyle~6 小时前
C++--- override 关键字 强制编译器验证当前函数是否重写基类的虚函数
java·前端·c++
HY小海6 小时前
【C++】AVL树实现
开发语言·数据结构·c++
仰泳的熊猫6 小时前
LeetCode:701. 二叉搜索树中的插入操作
数据结构·c++·算法·leetcode