We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a820bda commit 99c7ab2Copy full SHA for 99c7ab2
my-folder/problems/path_sum/solution.c
@@ -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