Minimum Depth of Binary Tree Java

原创 2015年07月08日 11:43:10

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int minDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        
        if (root.left == null && root.right == null) {
            return 1;
        }
        int minLeft=minDepth(root.left);
        int minRight=minDepth(root.right);
        if(minLeft == 0) {minLeft = Integer.MAX_VALUE; }
        if(minRight == 0){minRight = Integer.MAX_VALUE;}  
        return Math.min(minLeft,minRight)+1;
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode解题之Maximum/Minimum Depth of Binary Tree Java版(树的最大、最小深度)

leetcode解题之104# Maximum Depth of Binary Tree Java版(树的最大深度),111. Minimum Depth of Binary Tree(树的最大深度...

算法题——Minimum Depth of Binary Tree(JAVA)

题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along...

【LeetCode】111. Minimum Depth of Binary Tree解法及注释,Java,C++,DFS

111. Minimum Depth of Binary Tree  Total Accepted: 104964 Total Submissions: 342426 Difficulty: Ea...

Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 [java]

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the short...

[LeetCode][Java] Minimum Depth of Binary Tree

题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes al...

【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】

【111-Minimum Depth of Binary Tree(二叉树的最小深度)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tre...

LeetCode(111)Minimum Depth of Binary Tree

题目如下: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th...

【leetcode】111. Minimum Depth of Binary Tree

一、题目描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes...

LeetCode 111. Minimum Depth of Binary Tree(二叉树最小深度)

原题网址:https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its min...

Leetcode_111_Minimum Depth of Binary Tree

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41964249 Given a binary tree, ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)