递归
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root)return 0;
if(root->left==nullptr&&root->right==nullptr)return 1;
int ans=INT_MAX;
if(root->left){
ans=min(minDepth(root->left)+1,ans);
}
if(root->right){
ans=min(minDepth(root->right)+1,ans);
}
return ans;
}
};