[ Python ] | Simple & Clean Solution
Code\n\nclass Solution:\n    def pathSum(self, root: Optional[TreeNode], k: int) -> int:\n        def helper(root, currSum):\n            if not root: return \n
# Code\n```\nclass Solution:\n    def pathSum(self, root: Optional[TreeNode], k: int) -> int:\n        def helper(root, currSum):\n            if not root: return \n            \n            nonlocal paths\n\n            currSum += root.val\n            if m[currSum - k] != 0:\n                paths += m[currSum - k]\n\n            m[currSum] += 1\n            helper(root.left, currSum)\n            helper(root.right, currSum) \n            m[currSum] -= 1\n\n        paths = 0\n        m = defaultdict(int)\n        m[0] = 1\n        \n        helper(root, 0)\n\n        return paths\n```\n\n```\nclass Solution:\n    def pathSum(self, root: Optional[TreeNode], k: int) -> int:\n        def take(root, s):\n            if not root:\n                return 0\n            res = 0\n            if root.val == s: res += 1\n            res += take(root.left, s - root.val) + take(root.right, s - root.val)\n            return res\n        \n        if not root:\n            return 0\n        return self.pathSum(root.left, k) + self.pathSum(root.right, k) + take(root, k)\n```
['Tree', 'Depth-First Search', 'Binary Tree', 'Python3']