-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#75.cc
More file actions
28 lines (27 loc) · 783 Bytes
/
LeetCode#75.cc
File metadata and controls
28 lines (27 loc) · 783 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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
bool dfs(TreeNode* root, int sum, int target){
if(root==NULL) return false;
if(root->left==NULL && root->right==NULL){
if(sum + root->val ==target) return true;
else return false;
}
return dfs(root->left,sum+root->val,target) || dfs(root->right,sum+root->val,target);
}
public:
bool hasPathSum(TreeNode *root, int sum) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root==NULL) return false;
return dfs(root,0,sum);
}
};