avatar
y*n
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));
}
avatar
l*a
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;
: }

avatar
r*n
3
default of bool is 0

【在 l*****a 的大作中提到】
: what is the default value of bool in c++ if you don't initialize it
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。