```
public class Solution {
public int minDepth(TreeNode root) {
if(root == null) {
return 0;
}
if(root.left == null && root.right == null) {
return 1;
}
int min = Integer.MAX_VALUE;
int level = 0;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()) {
int size = queue.size();
level++;
for(int i = 0; i < size; i++) {
TreeNode temp = queue.poll();
if(temp.left == null && temp.right == null) {
min = Math.min(min, level);
return min;
}
if(temp.left != null) {
queue.offer(temp.left);
}
if(temp.right != null) {
queue.offer(temp.right);
}
}
}
return 0;
}
```

}