java刷题--111 二叉树的最小深度

Posted Anrys

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java刷题--111 二叉树的最小深度相关的知识,希望对你有一定的参考价值。

java刷题--111 二叉树的最小深度

题目

在这里插入图片描述

代码

class Solution {
    public int minDepth(TreeNode root) {
        if(root==null) return 0;
        if(root.left==null && root.right!=null) return 1+minDepth(root.right);
        if(root.right==null && root.left!=null) return 1+minDepth(root.left);
        return Math.min(minDepth(root.right), minDepth(root.left))+1;
    }
}

结果

在这里插入图片描述

以上是关于java刷题--111 二叉树的最小深度的主要内容,如果未能解决你的问题,请参考以下文章