-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPathSum.cpp
25 lines (23 loc) · 849 Bytes
/
PathSum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Problem-Link: https://leetcode.com/problems/path-sum/
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class PathSum {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
if (root == nullptr) return false;
if (targetSum == root->val && isLeaf(root)) return true;
return hasPathSum(root->left, targetSum - root->val) || hasPathSum(root->right, targetSum - root->val);
}
private:
bool isLeaf(TreeNode* node) {
if (node == nullptr) return false;
return node->left == node-> right && node->left == nullptr;
}
};