/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (root==NULL) {
return false;
}
int res = sum-root->val;
if (res==0 && root->left==NULL && root->right==NULL) {
return true;
}
bool leftSum = hasPathSum(root->left, res);
bool rightSum = hasPathSum(root->right, res);
if (leftSum || rightSum) {
return true;
} else {
return false;
}
}
};
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (root==NULL) {
return false;
}
int res = sum-root->val;
if (res==0 && root->left==NULL && root->right==NULL) {
return true;
}
bool leftSum = hasPathSum(root->left, res);
bool rightSum = hasPathSum(root->right, res);
if (leftSum || rightSum) {
return true;
} else {
return false;
}
}
};