y***n 发帖数: 1594 | 1 LeetCode says that I am failing the test case
Input: {1,2}, 0
Output: true
Expected: false
However, I can pass it with a console assert.
bool getPathSum(TreeNode *root, int pathSum, int sum){
pathSum +=root->val;
if(NULL==root->left && NULL==root->right){//get to the leaf
return pathSum==sum;
}
bool left, right;
if(root->left){
left=getPathSum(root->left, pathSum, sum);
}
if(root->right){
right=getPathSum(root->right, pathSum, sum);
}
return left||right;
}
bool hasPathSum(TreeNode *root, int sum) {
if(NULL==root) return false;
int pathSum=0;
return getPathSum(root, pathSum, sum);
}
int main(int argc, char *argv[]) {
TreeNode* root=new TreeNode(1);
root->left=new TreeNode(2);
root->right=NULL;
assert(false==hasPathSum(root, 0));
} | l*****a 发帖数: 14598 | 2 what is the default value of bool in c++ if you don't initialize it
【在 y***n 的大作中提到】 : LeetCode says that I am failing the test case : Input: {1,2}, 0 : Output: true : Expected: false : However, I can pass it with a console assert. : bool getPathSum(TreeNode *root, int pathSum, int sum){ : pathSum +=root->val; : if(NULL==root->left && NULL==root->right){//get to the leaf : return pathSum==sum; : }
| r*******n 发帖数: 3020 | 3 default of bool is 0
【在 l*****a 的大作中提到】 : what is the default value of bool in c++ if you don't initialize it
|
|