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;
    }
};
相关推荐
仰泳的熊猫8 分钟前
1108 Finding Average
数据结构·c++·算法·pat考试
AA陈超1 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 P07-18.生成火球术
c++·游戏·ue5·游戏引擎·虚幻
wxin_VXbishe1 小时前
springboot居家养老管理系统-计算机毕业设计源码55953
java·c++·spring boot·python·spring·django·php
ULTRA??1 小时前
归并排序算法实现,kotlin,c++,python
c++·python·kotlin
deng-c-f1 小时前
C/C++内置库函数(5):值/引用传递、移动构造、以及常用的构造技巧
开发语言·c++
qq_310658511 小时前
mediasoup源码走读(十)——producer
服务器·c++·音视频
Tipriest_1 小时前
C++ Python使用常用库时如何做欧拉角 ⇄ 四元数转换
c++·python·四元数·欧拉角
小尧嵌入式1 小时前
C语言中的面向对象思想
c语言·开发语言·数据结构·c++·单片机·qt
fpcc2 小时前
跟我学C++中级篇——循环展开的分析
c++·优化
埃伊蟹黄面2 小时前
算法 --- hash
数据结构·c++·算法·leetcode