```
public int MinDepth(TreeNode root) {
if(root == null) return 0;
Queue<TreeNode> queue = new Queue<TreeNode>();
Queue<int> queueLevel = new Queue<int>();
queue.Enqueue(root);
queueLevel.Enqueue(1);
while(queue.Count != 0)
{
TreeNode tmp = queue.Dequeue();
int level = queueLevel.Dequeue();
if(tmp.left == null && tmp.right == null) return level;
else
{
level++;
if(tmp.left != null)
{
queue.Enqueue(tmp.left);
queueLevel.Enqueue(level);
}
if(tmp.right != null)
{
queue.Enqueue(tmp.right);
queueLevel.Enqueue(level);
}
}
}
return 0;
}
```