Root to leaf path sum equal to a given number in a TREE
PROBLEM :
Given a Binary Tree and a sum s, your task is to check whether there is a root to leaf path in that tree with the following sum . You are required to complete the function hasPathSum. You should not read any input from stdin/console. There are multiple test cases. For each test case, this method will be called individually.
Input:
The task is to complete the function hasPathSum which takes 2 arguments, root of the Tree and a value sum. The struct node has a data part which stores the data, pointer to left child and pointer to right child.
There are multiple test cases. For each test case, this method will be called individually.
Output:
The function should return the true if such path exist else return false.
Constraints:
1 <=T<= 30
1 <=Number of nodes<= 100
1 <=Data of a node<= 1000
For example, in the above tree root to leaf paths exist with following sums.
21 –> 10 – 8 – 3
23 –> 10 – 8 – 5
14 –> 10 – 2 – 2
So the returned value should be true only for numbers 21, 23 and 14. For any other number, returned value should be false.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* A binary tree node
struct Node
{
int data;
struct Node* left, * right;
}; */
/*you are required to
complete this function */
bool hasPathSum(Node *root, int sum)
{
if(sum==0&&root==NULL)
return true ;
if(sum==0||root==NULL)
return false ;
sum=sum-root->data ;
if(root->left==NULL&&root->right==NULL)
{
if(sum==0)
return true ;
else
return false ;
}
else
return(hasPathSum(root->left,sum)||hasPathSum(root->right,sum)) ;
}
------------------------------------------------------------------------------
BETTER SOLUTION :
-------------------------------------------------------------------------------
/* A binary tree node
struct Node
{
int data;
struct Node* left, * right;
}; */
/*you are required to
complete this function */
bool hasPathSum(Node *root, int sum)
{
if(sum==0&&root==NULL)
return true ;
if(sum==0||root==NULL)
return false ;
sum=sum-root->data ;
if(root->left==NULL&&root->right==NULL)
{
if(sum==0)
return true ;
else
return false ;
}
if((root->left!=NULL||root->right!=NULL)&&sum<=0)
return false ;
else
return(hasPathSum(root->left,sum)||hasPathSum(root->right,sum)) ;
}
---------------------------------------------------------------------------------
Given a Binary Tree and a sum s, your task is to check whether there is a root to leaf path in that tree with the following sum . You are required to complete the function hasPathSum. You should not read any input from stdin/console. There are multiple test cases. For each test case, this method will be called individually.
Input:
The task is to complete the function hasPathSum which takes 2 arguments, root of the Tree and a value sum. The struct node has a data part which stores the data, pointer to left child and pointer to right child.
There are multiple test cases. For each test case, this method will be called individually.
Output:
The function should return the true if such path exist else return false.
Constraints:
1 <=T<= 30
1 <=Number of nodes<= 100
1 <=Data of a node<= 1000
For example, in the above tree root to leaf paths exist with following sums.
21 –> 10 – 8 – 3
23 –> 10 – 8 – 5
14 –> 10 – 2 – 2
So the returned value should be true only for numbers 21, 23 and 14. For any other number, returned value should be false.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* A binary tree node
struct Node
{
int data;
struct Node* left, * right;
}; */
/*you are required to
complete this function */
bool hasPathSum(Node *root, int sum)
{
if(sum==0&&root==NULL)
return true ;
if(sum==0||root==NULL)
return false ;
sum=sum-root->data ;
if(root->left==NULL&&root->right==NULL)
{
if(sum==0)
return true ;
else
return false ;
}
else
return(hasPathSum(root->left,sum)||hasPathSum(root->right,sum)) ;
}
------------------------------------------------------------------------------
BETTER SOLUTION :
-------------------------------------------------------------------------------
/* A binary tree node
struct Node
{
int data;
struct Node* left, * right;
}; */
/*you are required to
complete this function */
bool hasPathSum(Node *root, int sum)
{
if(sum==0&&root==NULL)
return true ;
if(sum==0||root==NULL)
return false ;
sum=sum-root->data ;
if(root->left==NULL&&root->right==NULL)
{
if(sum==0)
return true ;
else
return false ;
}
if((root->left!=NULL||root->right!=NULL)&&sum<=0)
return false ;
else
return(hasPathSum(root->left,sum)||hasPathSum(root->right,sum)) ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment