```
class Solution {
public:
int minDepth(TreeNode* root) {
return !root ? 0 : root->left && root->right ? min(minDepth(root->left), minDepth(root->right)) + 1 : root->left ? minDepth(root->left) + 1 : root->right ? minDepth(root->right) + 1 : 1;
}
};
```