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;
    }
};
相关推荐
旖-旎3 分钟前
分治(交易逆序对的总数)(6)
c++·算法·leetcode·排序算法·归并排序
北顾笙9803 分钟前
day14-数据结构力扣
数据结构·算法·leetcode
郝学胜-神的一滴4 分钟前
[简化版 GAMES 101] 计算机图形学 03:线性代数下
开发语言·c++·线性代数·图形渲染
一路向北he12 分钟前
esp32库依赖
c语言·c++·算法
Howrun77722 分钟前
C++ 项目测试全指南:从 0 基础到落地实操
开发语言·c++·log4j
YYYing.24 分钟前
【Linux/C++网络篇(二) 】TCP并发服务器演进史:从多进程到Epoll的进化指南
linux·服务器·网络·c++·tcp/ip
追光的蜗牛丿24 分钟前
C++传递参数时什么情况下传递引用
开发语言·javascript·c++
sheng420427 分钟前
小记近期C++遇到的坑
c++
wregjru29 分钟前
【高并发服务器项目】1.服务器接入层代码详解
c++
森G29 分钟前
41、数据库---------事件系统
c++·qt