forked from black-shadows/InterviewBit-Topicwise-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPathSum.cpp
More file actions
32 lines (26 loc) · 905 Bytes
/
PathSum.cpp
File metadata and controls
32 lines (26 loc) · 905 Bytes
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
// https://www.interviewbit.com/problems/path-sum/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
int checkSum(TreeNode* root, int sum){
if(root == NULL){
return 0;
}
if(root->val == sum && root->left == NULL && root->right == NULL){
return 1;
}
return max(checkSum(root->left, sum - root->val), checkSum(root->right, sum - root->val));
}
int Solution::hasPathSum(TreeNode* A, int B) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
return checkSum(A, B);
}