C++ 2 lines body code (with explanation)
Explanation for helper function f is below. The rest should be self-explanatory.\n\n    class Solution {\n    public:\n    int pathSum(TreeNode* root, int sum) 
Explanation for helper function `f` is below. The rest should be self-explanatory.\n```\n    class Solution {\n    public:\n    int pathSum(TreeNode* root, int sum) {\n        return root ? f(root, sum) + pathSum(root->left, sum) + pathSum(root->right, sum) : 0;\n    }\n    \n    int f(TreeNode* root, int sum) {\n        return root ? (root->val == sum) + f(root->left, sum - root->val) + f(root->right, sum - root->val) : 0;\n    }\n};\n```\n\nHelper function `f(root, sum)` = **path sum if we only consider paths beginning at root**\n\nFor example, for the following tree, `f(root, sum=6)` is `1` based on path `[5, 3, -2]`. \n\nAlthough path `[3, 3]` also sums to `6`, it's not counted because it doesn't begin with root `node 5`.\n```\n    5 \n   / \\ \n  3   2 \n / \\   \\\n3  -2   1\n```
[]