```
public class Solution {
public int MinDepth(TreeNode root) {
if (root == null) return 0;
if (root.right == null)
return MinDepth(root.left)+1;
else if (root.left == null)
return MinDepth(root.right)+1;
else
{
int i = MinDepth(root.left);
int j = MinDepth(root.right);
return Math.Min(i,j)+1;
}
}
}
```