Root to leaf path sum | Practice | GeeksforGeeks

Given a binary tree and an integer S, check whether there is root to leaf path with its sum as S.
Example 1:
Input:
Tree = 
            1
          /   \
        2      3
S = 2

Output: 0

Explanation:
There is no root to leaf path with sum 2.
Example 2:
Input:
Tree = 
            1
          /   \
        2      3
S = 4

Output: 1

Explanation:
The sum of path from leaf node 3 to root 1 is 4.

Your Task:  
You dont need to read input or print anything. Complete the function hasPathSum() which takes root node and target sum S as input parameter and returns true if path exists otherwise it returns false.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(height of tree)
Constraints:
1 ≤ N ≤ 10^4
1 ≤ S ≤ 10^6
0
harshscode1 week ago
if(root==NULL or root->data>s)       return 0;              if(root->left==NULL and root->right==NULL and root->data==s)       return true;              return hasPathSum(root->left,s-root->data) || hasPathSum(root->right,s-root->data);       
0
detroix07
This comment was deleted.
0
roopsaisurampudi1 month ago
boolean hasPathSum(Node root, int S) {
        // Your code here
        if (root == null) return false;
        if (root.data > S) return false;
        if (root.left == null && root.right == null) return (S==root.data);
        return hasPathSum(root.left, S-root.data) || hasPathSum(root.right, S-root.data);
    }
0
shankarmudiraj8271 month ago
JAVA SOLUTION
   class Solution {   /*you are required to complete this function */   static int   sum=0;   static boolean f=false;
   boolean hasPathSum(Node root, int S) {       sum=0;      f=false;       Solution s=new Solution();       if(root==null)       return true;       check(root,sum,S);       return f;   }    void check(Node root,int sum,int s){       if(root==null)       return;       if((root.left==null && root.right==null)&& (sum+root.data==s))       {           f=true;
       }       check(root.left,sum+root.data,s);       check(root.right,sum+root.data,s);   }}
0
ankukumar74242 months ago
 bool hasPathSum(Node *root, int S) {     if(root==NULL)      return false;      int subsum=S-root->data;      if(!root->left && !root->right && subsum==0)        return true;        bool a=hasPathSum(root->left,S-root->data);        bool b=hasPathSum(root->right,S-root->data);        return a||b;}
+1
gulashanshashishekhar292 months ago
bool hasPathSum(Node *root, int S) {
        // Your code here
        if (!root || root->data > S) return false;
        if (!root->left && !root->right && 
        	S == root->data) return true;
        return  hasPathSum(root->left, S - root->data) || 
                hasPathSum(root->right, S - root->data);
    }
0
mohankumarit20012 months ago
bool hasPathSum(Node *root, int S) {
        if(!root)return false;
        if(!root->left && !root->right && ((S-root->data)==0))return true;
        return hasPathSum(root->left,S-root->data) || hasPathSum(root->right,S-root->data);
    }
+1
aloksinghbais022 months ago
C++ solution having time complexity as O(N) and space complexity as O(Height) is as follows :- 
 
Execution Time :- 0.2 / 1.5 sec
 
bool helper(Node *node,int s,int target){       if(!node) return (false);              if(helper(node->left,node->data + s,target)) return (true);       if(helper(node->right,node->data + s,target)) return (true);              if(!node->left && !node->right && node->data + s == target) return (true);       return (false);   }      bool hasPathSum(Node *root, int S){       return helper(root,0,S);   }
-1
goodluckji20013 months ago
 bool solve(Node *root , int sum){       if(root==NULL || sum < 0) return false;               if(sum==root->data){         if(!root->right and !root->left)           return true;           else return false;       }               sum = sum - root->data;               return   (solve(root->left,sum) or solve(root->right,sum));       }         bool hasPathSum(Node *root, int S) {    return solve(root,S);}
0
manjaykumardpcse193 months ago


class Solution {
   boolean hasPathSum(Node root, int s) {
       if(root == null) return false;
 if(root.left == null && root.right == null && s - root.data == 0)              return true;
       return hasPathSum(root.left , s - root.data) || hasPathSum(root.right , s - root.data);
   }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.