# Share my simple Java solution

• ``````public class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
return minDepth(root, 1);
}

public int minDepth(TreeNode tn, int depth) {
if (tn.left == null && tn.right == null) {
return depth;
}
int left = Integer.MAX_VALUE;
int right = Integer.MAX_VALUE;
if (tn.left != null) {
left = minDepth(tn.left, depth+1);
}
if (tn.right != null) {
right = minDepth(tn.right, depth+1);
}
return Math.min(left, right);
}
}
``````

Basiclly, it is recursive solution where you calculate min depths of left and right child, and then take min of those values.

• I got the same idea, but put it in a single function. It seems a bit odd that the performance downgraded a bit, when I moved the (root == null) check out of the recursive function.

``````public int minDepth(TreeNode root) {
if(root == null){
return 0;
}

if(root.left == null && root.right == null){
return 1;
}

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

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.