-
Notifications
You must be signed in to change notification settings - Fork 1
/
112_Path_Sum.cpp
32 lines (27 loc) · 940 Bytes
/
112_Path_Sum.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool rec(TreeNode* root, int sum, int target){
if(root == NULL) return false;
if(sum == target && root->left == NULL && root->right == NULL) return true;
bool res1 = false, res2 = false;
if(root->left != NULL)
res1 = rec(root->left, sum + root->left->val, target);
if(root->right != NULL)
res2 = rec(root->right, sum + root->right->val, target);
return res1 | res2;
}
bool hasPathSum(TreeNode* root, int sum) {
if(root == NULL) return false;
if(root->val == sum && root->left == NULL && root->right == NULL) return true;
return rec(root, root->val, sum);
}
};