class Solution {
public:
int run(TreeNode *root) {
if(root==NULL)
return 0;
else if(root->left==NULL&&root->right==NULL)
return 1;
else if(root->left!=NULL&&root->right==NULL)
return run(root->left)+1;
else if(root->left==NULL&&root->right!=NULL)
return run(root->right)+1;
int leftmin = run(root->left);
int rightmin = run(root->right);
return (leftmin<rightmin)?(leftmin+1):(rightmin+1);
}
};
class Solution
{
public:
int run(TreeNode *root){
if(!root)
return 0;
int l = run(root->left);
int r = run(root->right);
if(l==0||r==0)
return 1 + l +r;
else
return (l<r)?(l+1):(r+1);
}
};
思路都是一样的 我不明白为什么第一段代码在牛客网上没有通过 一直提示未定义的leftmin 和 rightmin
第二段通过了
另外注意要在返回出加一