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