Coding Ninjas Logo

Home > Data Structures and Algorithms Questions > Minimum Depth of a Binary Tree

Minimum Depth of a Binary Tree



Answer:

Here's a code snippet find minimum depth of a Binary Tree

public int minDepth(TreeNode root){

  if(root == null){
     return 0;
  }
  return helper(root);
}

public int helper(TreeNode root) {

  if(root == null){
    return Integer.MAX_VALUE;
  }
  if(root.left == null && root.right == null){
    return 1;
  }
  int depthLeft = helper(root.left);
  int depthRight = helper(root.right);
  return Math.min(depthLeft, depthRight) + 1;
  }
}


Similar Questions