-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSum.c
More file actions
37 lines (35 loc) · 725 Bytes
/
PathSum.c
File metadata and controls
37 lines (35 loc) · 725 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
33
34
35
36
37
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool AddValue(struct TreeNode* node, int pre_sum, int sum)
{
int result_1 = 0,result_2 = 0;
pre_sum += node->val;
if(node->left == NULL && node->right == NULL)
{
return pre_sum == sum;
}
if(node->left != NULL)
{
result_1 = AddValue(node->left, pre_sum, sum);
}
if(node->right != NULL)
{
result_2 = AddValue(node->right, pre_sum, sum);
}
return result_1 | result_2;
}
bool hasPathSum(struct TreeNode* root, int sum)
{
int pre_sum = 0;
if(root == NULL)
{
return false;
}
return AddValue(root, pre_sum, sum);
}