We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 033ac55 + 7d57e4f commit 8ffa046Copy full SHA for 8ffa046
1 file changed
LeetCode/hasPathSum.cpp
@@ -0,0 +1,17 @@
1
+/**
2
+ * Definition for binary tree
3
+ * struct TreeNode {
4
+ * int val;
5
+ * TreeNode *left;
6
+ * TreeNode *right;
7
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8
+ * };
9
+ */
10
+class Solution {
11
+public:
12
+ bool hasPathSum(TreeNode *root, int sum) {
13
+ if (root == NULL) return false;
14
+ if (root->left == NULL && root->right == NULL && root->val == sum) return true;
15
+ return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
16
+ }
17
+};
0 commit comments