Skip to content

Commit e2c4ffc

Browse files
committed
path sum
1 parent 9f60104 commit e2c4ffc

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
+ [100 Same Tree](algorithms/SameTree)
3131
+ [101 Symmetric Tree](algorithms/SymmetricTree)
3232
+ [104 Maximum Depth of Binary Tree](algorithms/MaximumDepthofBinaryTree)
33+
+ [112 Path Sum](algorithms/PathSum)
3334
+ [136 Single Number(位运算)](algorithms/SingleNumber)
3435
+ [141 Linked List Cycle](algorithms/LinkedListCycle)
3536
+ [142 Linked List Cycle II](algorithms/LinkedListCycle2)

algorithms/PathSum/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Path Sum
2+
3+
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
4+
5+
For example:
6+
Given the below binary tree and `sum = 22`,
7+
```
8+
5
9+
/ \
10+
4 8
11+
/ / \
12+
11 13 4
13+
/ \ \
14+
7 2 1
15+
```
16+
return true, as there exist a root-to-leaf path `5->4->11->2` which sum is 22.
17+
18+
## Solution
19+
20+
递归题
21+
22+
* 如果p是叶子节点,则若`p的值等于sum``return true`, 否则`return false`
23+
* 若p不是叶子节点,则一定存在孩子节点,左孩子或者右孩子满足其中一个和为`sum - p->val`即可,即
24+
`return hasPath(p->left, sum - p->val) || hasPath(p->right, sum - p->right)`
25+
26+
## Code
27+
```c
28+
bool hasPathSum(struct TreeNode *root, int sum) {
29+
if (root == NULL)
30+
return false;
31+
if (root->left == NULL && root->right == NULL)
32+
return root->val == sum;
33+
if (!root->left || !hasPathSum(root->left, sum - root->val)) {
34+
return hasPathSum(root->right, sum - root->val);
35+
}
36+
return true;
37+
}
38+
```

algorithms/PathSum/solve.c

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <stdio.h>
2+
#include <stdbool.h>
3+
#include <stdlib.h>
4+
5+
struct TreeNode {
6+
int val;
7+
struct TreeNode *left;
8+
struct TreeNode *right;
9+
};
10+
bool hasPathSum(struct TreeNode *root, int sum) {
11+
if (root == NULL)
12+
return false;
13+
if (root->left == NULL && root->right == NULL)
14+
return root->val == sum;
15+
if (!root->left || !hasPathSum(root->left, sum - root->val)) {
16+
return hasPathSum(root->right, sum - root->val);
17+
}
18+
return true;
19+
}
20+
21+
struct TreeNode *mk_node(int val)
22+
{
23+
struct TreeNode *p = malloc(sizeof(*p));
24+
p->val = val;
25+
p->left = p->right = NULL;
26+
return p;
27+
}
28+
void mk_child(struct TreeNode *root, struct TreeNode *left, struct TreeNode *right)
29+
{
30+
root->left = left;
31+
root->right = right;
32+
}
33+
int main(int argc, char **argv)
34+
{
35+
struct TreeNode *root = mk_node(5);
36+
mk_child(root, mk_node(4), mk_node(8));
37+
struct TreeNode *left = root->left;
38+
struct TreeNode *right = root->right;
39+
mk_child(left, mk_node(11), NULL);
40+
mk_child(left->left, mk_node(7), mk_node(2));
41+
mk_child(right, mk_node(13), mk_node(4));
42+
mk_child(right->right, NULL, mk_node(1));
43+
printf("%d\n", hasPathSum(root, 22));
44+
printf("%d\n", hasPathSum(root, 26));
45+
printf("%d\n", hasPathSum(root, 18));
46+
printf("%d\n", hasPathSum(root, 27));
47+
printf("%d\n", hasPathSum(root, 37));
48+
printf("%d\n", hasPathSum(root, 100));
49+
printf("%d\n", hasPathSum(root, 0));
50+
return 0;
51+
}

0 commit comments

Comments
 (0)