-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhas_path_sum_equal_to_a_value.cpp
42 lines (32 loc) · 1.1 KB
/
has_path_sum_equal_to_a_value.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
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.
https://practice.geeksforgeeks.org/problems/root-to-leaf-path-sum/1
*/
-------------------------------------------------------------------------------------------------------------------------------
bool hasPathSum_sec(struct Node* node, int sum,int subSum)
{
bool ans;
if (node == NULL)
{
return 0;
}
else
{
ans = 0;
/* otherwise check both subtrees */
subSum += node->data;
/* If reach a leaf node and sum becomes 0 then return true*/
if ( subSum == sum && node->left == NULL && node->right == NULL )
return 1;
if ( subSum != sum && node->left == NULL && node->right == NULL )
return 0;
ans = ans || hasPathSum_sec(node->left,sum,subSum) || hasPathSum_sec(node->right,sum, subSum);
}
return ans;
}
bool hasPathSum(struct Node* node, int sum)
{
/* return true if we run out of tree and sum==0 */
return hasPathSum_sec(node,sum,0);
}