Skip to content

Commit a0991ad

Browse files
committed
add LeetCode 112
1 parent 313d473 commit a0991ad

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

c/LeetCode/112.path-sum.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
10+
11+
bool hasPathSum(struct TreeNode* root, int sum){
12+
13+
if (root == NULL) {
14+
return 0;
15+
}
16+
17+
if (!root->left && !root->right) {
18+
return sum == root->val;
19+
}
20+
21+
if (!root->left && root->right) {
22+
return hasPathSum(root->right, sum - root->val);
23+
}
24+
25+
if (root->left && !root->right) {
26+
return hasPathSum(root->left, sum - root->val);
27+
}
28+
29+
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
30+
}
31+

0 commit comments

Comments
 (0)