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 // IMPORTANT: Please reset any member data you declared, as 14 // the same Solution instance will be reused for each test case. 15 if (root == NULL) 16 return false; 17 if (root->left == NULL && root->right ==NULL && root->val == sum) 18 return true; 19 return hasPathSum(root->left,sum-root->val) || hasPathSum(root->right,sum-root->val); 20 } 21 };