Skip to content

Commit 8ffa046

Browse files
committed
Merge branch 'master' of https://github.com/daiwb/Algorithm
2 parents 033ac55 + 7d57e4f commit 8ffa046

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

LeetCode/hasPathSum.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)