由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - hasPathSum
相关主题
问一个题目拓扑排序的题怎么做?
Time complexity一道面试题
写了个symmetric tree的stack based iterative实现,有个bugleetcode valid bst new test cases 过不去了。。。
判断是不是binary search tree-leetcodeInterview question::
G家电面GOOG ONSITE 面试
path sum II OJ 超时判断BT是否BST?
懒得写了,想练手的就写写贴在这里吧算法题:如何判断二叉树是AVL?
large file的一道题请教这么一个题:BST maximum sum path
相关话题的讨论汇总
话题: root话题: pathsum话题: treenode话题: haspathsum话题: null
进入JobHunting版参与讨论
1 (共1页)
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
1 (共1页)
进入JobHunting版参与讨论
相关主题
请教这么一个题:BST maximum sum pathG家电面
为啥有两个case不对??Binary Tree Maximum Path Sumpath sum II OJ 超时
Uni_value subtree problem懒得写了,想练手的就写写贴在这里吧
贴个自己的答案:Binary Tree Max Path Sumlarge file的一道题
问一个题目拓扑排序的题怎么做?
Time complexity一道面试题
写了个symmetric tree的stack based iterative实现,有个bugleetcode valid bst new test cases 过不去了。。。
判断是不是binary search tree-leetcodeInterview question::
相关话题的讨论汇总
话题: root话题: pathsum话题: treenode话题: haspathsum话题: null