1
class Solution {
2
public:
3
bool ans = false;
4
int sum = 0;
5
void recur(TreeNode *root, int target) {
6
if (root == NULL) return;
7

8
sum += root->val;
9
recur(root->left, target);
10
recur(root->right, target);
11
if (root->left == NULL && root->right == NULL &&
12
sum == target) { // !!Check only if it is a leaf node....
13
ans = true;
14
return;
15
}
16
sum -= root->val; // backtracking
17
return;
18
}
19
bool hasPathSum(TreeNode *root, int targetSum) {
20
recur(root, targetSum);
21
return ans;
22
}
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0