标签:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
int indicator = 0;
public:
void check(TreeNode *root, int sum) {
if(root == NULL)
return;
if(root->left == NULL && root->right == NULL) {
if(root->val == sum)
indicator = 1;
return;
}
if(root->left == NULL) {
check(root->right, sum - root->val);
return;
}
if(root->right == NULL) {
check(root->left, sum - root->val);
return;
}
check(root->left, sum - root->val);
check(root->right, sum - root->val);
}
bool hasPathSum(TreeNode* root, int sum) {
check(root, sum);
if(indicator == 1)
return true;
else
return false;
}
};
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/guanzhongshan/article/details/46746467