Minimum Depth of Binary Tree

http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ 

This question is pretty similar to the solution of Maximum Depth of Binary Tree, the only difference is that apart from the if condition (base case) to determine wheter the current Node is Null , two additional conditions are needed to determine whether the child Nodes are Null , because only from the Child Nodes can well determine whether they are leaf nodes or not.

Solution:

  public int minDepth(TreeNode root) {
        if (root == null) return 0;
        if (root.left == null) return 1 + minDepth(root.right);
        if (root.right == null) return 1 + minDepth(root.left);
        else {
            return 1+Math.min(minDepth(root.left),minDepth(root.right));
        } 
    }
原文地址:https://www.cnblogs.com/midan/p/4609313.html