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;
    }
};
相关推荐
凯瑟琳.奥古斯特32 分钟前
力扣1009补码解法C++实现
开发语言·c++·算法·leetcode·职场和发展
zh_xuan3 小时前
c++ optional用法
开发语言·c++·optional
Let's Chat Coding5 小时前
身份识别、身份认证与授权的区别
c++
鹏易灵5 小时前
C++——8.移动语义初探(移动构造、移动赋值)
开发语言·c++·php
凯瑟琳.奥古斯特6 小时前
力扣1008:前序重建BST
开发语言·c++·算法·leetcode·职场和发展
Alvin's Tech Blog6 小时前
内存泄漏会导致操作系统永久丢失内存吗?它会导致应用程序崩溃吗?
c++·内存管理·内存泄漏
会周易的程序员6 小时前
使用 LuaBridge 封装 C++ 日志库 microLog 为 Lua 模块
c++·物联网·junit·lua·日志·iot·aiot
ysa0510306 小时前
【板子】kmp
c++·笔记·算法·板子
wbs_scy6 小时前
仿 muduo 高并发服务器项目:从 timerfd 到时间轮实现定时任务机制
linux·服务器·c++
aqiu1111116 小时前
【算法日记 13】LeetCode 49. 字母异位词分组:哈希表的进阶“降维打击”
算法·leetcode·散列表