-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPathSum.cpp
42 lines (34 loc) · 813 Bytes
/
PathSum.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
#include <cassert>
#include <cstddef>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
//----------------------------------leetcode---------------------------------
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
return _hashPathSum(root, sum, 0);
}
private:
bool _hashPathSum(TreeNode* node, int sum, int currSum)
{
if (node == nullptr) return false;
currSum += node->val;
if (currSum == sum
&& node->left == nullptr
&& node->right == nullptr)
return true;
if (_hashPathSum(node->left, sum, currSum)
|| _hashPathSum(node->right, sum, currSum))
return true;
return false;
}
};
//----------------------------------leetcode---------------------------------
int main()
{
return 0;
}