<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>the number of paths where the sum of the values&nbsp;along the path equals</em>&nbsp;<code>targetSum</code>.</p>

<p>The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg" style="width: 450px; height: 386px;" />
<pre>
<strong>Input:</strong> root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
<strong>Output:</strong> 3
<strong>Explanation:</strong> The paths that sum to 8 are shown.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[0, 1000]</code>.</li>
	<li><code>-10<sup>9</sup> &lt;= Node.val &lt;= 10<sup>9</sup></code></li>
	<li><code>-1000 &lt;= targetSum &lt;= 1000</code></li>
</ul>

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:
      # 09/12/24 meta phone screen prep
      # same as the phone number pattern, walk through the tree
      # and then record answer when you ahve one.
      # 
      # on top of that , it is 560, prefix sum
      # in 560, you can remove that h[0] = [-1] and add the if cur_sum == target_sum check
      # but here you can only do the cur_sum == target_sum check since

        # 9/9/24 meta phone screen prep
        # prefix sum in the tree see the tutorial

    
        self.h = defaultdict(int)
        self.h[0] = 1
        self.cnt = 0
        
        def dfs(root, cur_sum):
            # the base condition
            if not root:
                return
            cur_sum += root.val

            # if cur_sum == targetSum: # the path starts from the root
            #     self.cnt += 1

            # the path starts somewhere in the middle
            self.cnt += self.h[cur_sum - targetSum]

            self.h[cur_sum] += 1
            dfs(root.left, cur_sum)
            dfs(root.right, cur_sum)

            self.h[cur_sum] -= 1 # see editorial for why you need to remove it. 

        dfs(root, 0)
        return self.cnt


        # 11/23/2021 facebbok 2nd prep
        
        # needs information from both parents and children
        # the path can end or start at any root because.
        
        self.res = 0
        
     
        # and meanwhile calculating path rum
        def dfs(root, sum_from_parent):
            if root:
                new_sum = sum_from_parent + root.val 
                # path that ends at root
                
                # if sum_from_parent == targetSum:
                #     self.res += 1
                if new_sum == targetSum:
                    self.res += 1
                # keep looking for path that contains the root
                dfs(root.left, new_sum)
                dfs(root.right, new_sum)
                # also looking for path that starts from the children of root
                dfs(root.left, 0)
                dfs(root.right, 0)
                
        dfs(root, 0)
        return self.res
                    
17271