Skip to content

Commit 99c7ab2

Browse files
committed
Sync LeetCode submission - Path Sum (c)
1 parent a820bda commit 99c7ab2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
int isLeaf(struct TreeNode*);
10+
11+
int hasPathSum(struct TreeNode* root, int targetSum) {
12+
// If we don't have a tree theres no path with the sum
13+
if (!root) {
14+
return 0;
15+
}
16+
// Stop if we hit a leaf
17+
if (isLeaf(root)) {
18+
return root->val == targetSum;
19+
}
20+
21+
return hasPathSum(root->left, targetSum - root->val) || hasPathSum(root->right, targetSum - root->val);
22+
}
23+
24+
int isLeaf(struct TreeNode* node) {
25+
return node && !node->left && !node->right;
26+
}

0 commit comments

Comments
 (0)