Skip to content

Commit a04b7ed

Browse files
authored
Create main.cpp
1 parent 1d01575 commit a04b7ed

File tree

1 file changed

+25
-0
lines changed
  • 17 - Binary Tree Data Structure Problems/12 - Path Sum

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
bool hasPathSum(TreeNode* root, int targetSum) {
15+
if(root == NULL) return false;
16+
17+
targetSum -= root -> val;
18+
19+
if(root -> left == NULL && root -> right == NULL){
20+
return targetSum == 0;
21+
}
22+
23+
return hasPathSum(root -> left, targetSum) || hasPathSum(root -> right, targetSum);
24+
}
25+
};

0 commit comments

Comments
 (0)