Minimum Depth of Binary Tree

题目地址:
https://leetcode.com/problems/minimum-depth-of-binary-tree/#/description

题目:
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.

解题思路:
这道题主要就是从左边要一个值,从右边要一个值,然后比较大小。

代码:

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

Comments

Popular Posts