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;
    }
};
相关推荐
前进的李工13 分钟前
LeetCode hot100:234 回文链表:快慢指针巧判回文链表
python·算法·leetcode·链表·快慢指针·回文链表
sin_hielo18 分钟前
leetcode 3228
算法·leetcode
xier_ran1 小时前
力扣(LeetCode)100题:41.缺失的第一个正数
数据结构·算法·leetcode
Elias不吃糖1 小时前
epoll 事件全集、每个事件的含义、哪些事件在实际服务器中最常见、哪些会组合出现
linux·c++·event
AA陈超1 小时前
ASC学习笔记0017:返回此能力系统组件的所有属性列表
c++·笔记·学习·ue5·虚幻引擎
Swift社区2 小时前
LeetCode 425 - 单词方块
算法·leetcode·职场和发展
Unlyrical2 小时前
splice, io_uring_prep_splice 调用(无效参数)
linux·服务器·c++·unix
Lucis__3 小时前
STL设计模式探秘:容器适配器&仿函数
c++·容器·stl·仿函数
无敌最俊朗@3 小时前
C++ 对象布局之padding(填充字节)
开发语言·c++
Miraitowa_cheems3 小时前
LeetCode算法日记 - Day 104: 通配符匹配
linux·数据结构·算法·leetcode·深度优先·动态规划