面试算法-73-二叉树的最小深度

题目

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明:叶子节点是指没有子节点的节点。

示例 1:

输入:root = [3,9,20,null,null,15,7]

输出:2

java 复制代码
class Solution {
    public int minDepth(TreeNode root) {
        if(root == null){
            return 0;
        }

        if(root.left == null && root.right == null){
            return 1;
        }

        if(root.left == null){
            return minDepth(root.right) +1;
        }

        if(root.right == null){
            return minDepth(root.left) +1;
        }
        
        return Math.min(minDepth(root.left),minDepth(root.right)) + 1;
    }
}
相关推荐
2401_8747325321 小时前
泛型编程与STL设计思想
开发语言·c++·算法
季远迩21 小时前
54.螺旋矩阵(中等)
算法
今儿敲了吗21 小时前
44| 汉诺塔问题
数据结构·c++·笔记·学习·算法·深度优先
米粒121 小时前
力扣算法刷题 Day 15
算法·leetcode·职场和发展
程序员小崔日记1 天前
一道基础计算题卡在 40 分,求助判题规则问题
java·算法·竞赛
愣头不青1 天前
543.二叉树的直径
java·算法
此方ls1 天前
机器学习聚类算法二——DBSCAN(Density-Based Spatial Clustering of Applications with Noise)
算法·机器学习·聚类
add45a1 天前
C++中的原型模式
开发语言·c++·算法
2401_844221321 天前
C++类型推导(auto/decltype)
开发语言·c++·算法
2201_753877791 天前
高性能计算中的C++优化
开发语言·c++·算法