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))
		);
}
Exemple #3
0
 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));
 }
Exemple #4
0
 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));
 }
Exemple #5
0
 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;
     
 }
Exemple #6
0
 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);
}
Exemple #8
0
 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);
     }
 }
Exemple #10
0
 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;
 }
Exemple #11
0
/*
 * 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;
}
Exemple #12
0
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);
}
Exemple #13
0
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));
	
}
Exemple #14
0
 // 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));
 }
Exemple #15
0
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;
}
Exemple #16
0
		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);
		}
Exemple #17
0
    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;
 }
Exemple #19
0
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;
}
Exemple #20
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);
 }
Exemple #21
0
 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);
 }
Exemple #22
0
/*
* 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));
   }
}
Exemple #23
0
	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;
	}
Exemple #24
0
/* 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));
	}
}
Exemple #25
0
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);
  }
Exemple #27
0
/**
 * 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);
}
Exemple #28
0
 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);
 }
Exemple #30
0
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);
}