1class Solution {2public:3bool ans = false;4int sum = 0;5void recur(TreeNode *root, int target) {6if (root == NULL) return;78sum += root->val;9recur(root->left, target);10recur(root->right, target);11if (root->left == NULL && root->right == NULL &&12sum == target) { // !!Check only if it is a leaf node....13ans = true;14return;15}16sum -= root->val; // backtracking17return;18}19bool hasPathSum(TreeNode *root, int targetSum) {20recur(root, targetSum);21return ans;22}23};