int hasPathSum(struct node* n, int sum) { int stemp=0; if(n != NULL) { sum = sum - n->data; if(n->left != NULL || n->right != NULL) { if(n->right == NULL) { if(hasPathSum(n->left,sum) == 1) return 1; } else if(n->left == NULL) { if(hasPathSum(n->right,sum) == 1) return 1; } else { if(hasPathSum(n->left,sum) == 1) return 1; if(hasPathSum(n->right,sum) == 1) return 1; } } if(sum == 0) return 1; else return -1; } }
int hasPathSum(Node *root,int sumValue,int parentSumVal) { static char firstCall = 'Y'; // Set parentSumval to 0 for first time call if (firstCall == 'Y') { parentSumVal = 0; firstCall = 'N'; } // Sum false if no root if (!root) { return 0; } // Compute the sum of current path including this node int currentPathSum = root->data + parentSumVal; // if sum value matching return true if ( !root->leftChild && !root->rightChild && currentPathSum == sumValue) { return 1; } return ( (hasPathSum(root->leftChild,sumValue,currentPathSum)) || (hasPathSum(root->rightChild,sumValue,currentPathSum)) ); }
bool hasPathSum(TreeNode* root, int sum) { if (!root) return false; else if (root->left == NULL && root->right == NULL) // leaf return root->val == sum; else // non-leaf return (hasPathSum(root->left, sum-root->val) || hasPathSum(root->right, sum-root->val)); }
bool hasPathSum(TreeNode *root, int sum) { if (!root) {return false;} // root->left == NULL && root->right == NULL, then it is a leaf node if ((root->left == NULL) && (root->right == NULL) && (sum - root->val == 0)) {return true;} return (hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val)); }
bool hasPathSum(TreeNode *root, int sum) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root == NULL) return false; if(root->left == NULL && root->right == NULL) { if(root->val == sum) { return true; } return false; } bool ret = false; if(root->left) { ret = hasPathSum(root->left, sum-root->val); } if(!ret && root->right) { ret = hasPathSum(root->right, sum-root->val); } return ret; }
bool hasPathSum(TreeNode* root, int sum) { if (!root) return false; if (!root->left && !root->right && root->val == sum) // leaf means both left and right child are NULL return true; return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); }
bool hasPathSum(node *root, int sum){ if(root == NULL || sum<0) return false; if(root->left == NULL && root->right == NULL && sum == root->data) return true; return hasPathSum(root->left, sum-root->data) || hasPathSum(root->right, sum-root->data); }
bool hasPathSum(TreeNode* root, int sum) { if(root == NULL) return false; if (root->left == NULL and root->right == NULL) return sum == root->val; return hasPathSum(root->left, sum - root->val) or hasPathSum(root->right, sum - root->val); }
bool hasPathSum(TreeNode* root, int sum) { if(!root) return false; if(!root->left && !root->right) return sum == root->val; else{ return hasPathSum(root->right, sum-root->val) || hasPathSum(root->left, sum-root->val); } }
bool hasPathSum(TreeNode* root, int sum) { if (root == NULL) return false; else if (root->val == sum && root->left == NULL && root->right == NULL) return true; if (root->left && hasPathSum(root->left, sum - root->val)) return true; if (root->right && hasPathSum(root->right, sum - root->val)) return true; return false; }
/* * I * */ bool hasPathSum(TreeNode *root, int sum){ if(!root) return false; if(root->left == NULL && root->right == NULL && sum == 0) return true; sum -= root->val; if(root->left != NULL && hasPathSum(root->left, sum)) return true; if(root->right != NULL && hasPathSum(root->right, sum)) return true; return false; }
bool hasPathSum(TreeNode *root, int num){ if(root==NULL) return false; if(sum==root->val && !(root->left || root->right)) return true; else return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); }
bool hasPathSum(Node *root, int sum) { if (!root) { return false; } if (isLeaf(root) && sum == root->data) return true; return (hasPathSum(root->right, sum- root->data)|| hasPathSum(root->left, sum - root->data)); }
// straight-forward implementation bool hasPathSum(TreeNode *root, int sum) { if(root == NULL) return false; if(root->left == NULL && root->right == NULL){ return root->val == sum ? true : false; } return (hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val)); }
bool hasPathSum(Node *node, int sum) { if(node->left==NULL && node->right==NULL) return sum==(node->data); if(node->left!=NULL && hasPathSum(node->left, sum - node->data)) return true; if(node->right!=NULL && hasPathSum(node->right, sum - node->data)) return true; return false; }
bool hasPathSum(TreeNode* root, int sum) { if (root == NULL) return false; if (root->left == NULL && root->right == NULL) return sum == root->val; else if (root->left == NULL) return hasPathSum(root->right, sum - root->val); else if (root->right == NULL) return hasPathSum(root->left, sum - root->val); return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); }
bool hasPathSum(TreeNode* root, int sum) { if (root == nullptr) return false; if (root->left == nullptr && root->right == nullptr) return sum == root->val; return (hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val)); }
bool hasPathSum(TreeNode* root, int sum) { if(root) { if(!root->left && !root->right && sum==root->val) return true; return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); } return false; }
int main(int argc, const char * argv[]) { TreeNode *root1 = TreeCreate(2,1,2); std::cout << hasPathSum(root1, 1) << std::endl; TreeNode *root2 = TreeCreate(15,5,4,8,11,INT_MAX,13,4,7,2,INT_MAX,INT_MAX,INT_MAX,INT_MAX,INT_MAX,1); std::cout << hasPathSum(root2, 22) << std::endl; return 0; }
bool hasPathSum(TreeNode *root, int sum) { if (!root) return false; else if (root->val == sum && !root->left && !root->right) return true; else return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); }
bool hasPathSum(TreeNode *root, int sum) { if (NULL == root) return false; if ((NULL == root->left) && (NULL == root->right)) // leaf node return (sum == root->val); return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); }
/* * Given a tree and a sum, return true if there is a path from the root * down to a leaf, such that adding up all the values along the path * equals the given sum. */ int hasPathSum(struct node* node, int sum){ if (NULL == node) return (sum == 0); else{ int subSum = sum - node->data; return (hasPathSum(node->left, subSum) || hasPathSum(node->right, subSum)); } }
bool hasPathSum(TreeNode *root, int sum, int curr_sum) { curr_sum += root->val; if(root->left == NULL && root->right == NULL && sum == curr_sum) return true; else if( (root->left && hasPathSum(root->left, sum, curr_sum)) || (root->right && hasPathSum(root->right, sum, curr_sum))) return true; return false; }
/* Given a tree and a sum, return true if there is a path from the root down to a leaf, such that adding up all the values along the path equals the given sum. */ int hasPathSum(struct node* root, int sum) { if(root == 0) return sum ==0; else { int newsum = sum - root->data; return (hasPathSum(root->left,newsum) || hasPathSum(root->right,newsum)); } }
bool hasPathSum(TreeNode* root, int sum) { if (!root) return 0; else if (root->left == NULL && root->right == NULL) return sum == root->val; else return (hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val)); }
bool hasPathSum(TreeNode *root, int sum) { if (!root) return false; if (root->left == nullptr && root->right == nullptr) { if (root->val == sum) return true; } int new_sum = sum - root->val; return hasPathSum(root->left, new_sum) || hasPathSum(root->right, new_sum); }
/** * method 1: recursion. */ bool hasPathSum(struct TreeNode* root, int sum) { if (root == NULL) return 0; if (root->left == NULL && root->right == NULL) { return (sum == root->val); } return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); }
bool hasPathSum(TreeNode* root, int sum) { if(root == NULL) { return false; } if(root->val == sum && !root->left && !root->right) { return true; } return hasPathSum(root->left, sum-root->val) || hasPathSum(root->right, sum-root->val); }
bool hasPathSum(TreeNode *root, int sum) { if (root == NULL) return false; // leaf node else if (root->left == NULL && root->right == NULL) return root->val == sum; else return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); }
int hasPathSum(struct TreeNode* root, int sum) { if(root == NULL) return 0; else if(root->left==NULL && root->right==NULL) return sum==root->val; return hasPathSum(root->left, sum-root->val) || hasPathSum(root->right, sum-root->val); }