```
int minDepth(TreeNode *root) {
if (root == NULL)
{
return 0;
}
int left = minDepth(root->left);
int right = minDepth(root->right);
if (left == 0 && right == 0) // leaf
{
return 1;
} else if (left == 0)
{
return 1 + right;
} else if (right == 0)
{
return 1 + left;
} else
{
return 1 + min(left, right);
}
}
```

My solution is O(n) in time and O(1) in space. This is done using pre-order traversal, where we recurse through the whole tree and then determine what the minimum depth would be.

Can we do any better?