For tree structure problems. recursion is usually intuitive and easy to write. lol

```
class Solution {
public:
int pathSum(TreeNode* root, int sum) {
if(!root) return 0;
return sumUp(root, 0, sum) + pathSum(root->left, sum) + pathSum(root->right, sum);
}
private:
int sumUp(TreeNode* root, int pre, int& sum){
if(!root) return 0;
int current = pre + root->val;
return (current == sum) + sumUp(root->left, current, sum) + sumUp(root->right, current, sum);
}
};
```