For a leetcode problem to find the minimum depth of the binary tree, I have written the below solutions. The solution is accepted, but can I do any other changes to make the code elegent?
public class Solution {
public int minDepth(TreeNode root) {
if(root == null)
{
return 0;
}
//to handle the leaf node case...
if(root.left == null && root.right == null)
{
return 1;
}
//To handle the case that has only right children..
if(root.left == null)
{
return minDepth(root.right) + 1;
}
if(root.right == null)
{
return minDepth(root.left) + 1;
}
return Math.min( minDepth(root.left)+1,minDepth(root.right)+1);
}
}
if
s are unnecessary, if you remove them the function will work just the same. \$\endgroup\$