Minimum Depth of a binary tree

 class Solution {

    public int minDepth(TreeNode root) {        

        if (root == null) return 0;

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;        

    }

}



Comments

Popular posts from this blog

Java 8 : Find the number starts with 1 from a list of integers

Optional Vs Null Check

How to prevent Singleton Class from Reflection and Serialization