<p>Given the <code>root</code> of a binary tree, replace the value of each node in the tree with the <strong>sum of all its cousins&#39; values</strong>.</p>

<p>Two nodes of a binary tree are <strong>cousins</strong> if they have the same depth with different parents.</p>

<p>Return <em>the </em><code>root</code><em> of the modified tree</em>.</p>

<p><strong>Note</strong> that the depth of a node is the number of edges in the path from the root node to it.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/11/example11.png" style="width: 571px; height: 151px;" />
<pre>
<strong>Input:</strong> root = [5,4,9,1,10,null,7]
<strong>Output:</strong> [0,0,0,7,7,null,11]
<strong>Explanation:</strong> The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
- Node with value 5 does not have any cousins so its sum is 0.
- Node with value 4 does not have any cousins so its sum is 0.
- Node with value 9 does not have any cousins so its sum is 0.
- Node with value 1 has a cousin with value 7 so its sum is 7.
- Node with value 10 has a cousin with value 7 so its sum is 7.
- Node with value 7 has cousins with values 1 and 10 so its sum is 11.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/11/diagram33.png" style="width: 481px; height: 91px;" />
<pre>
<strong>Input:</strong> root = [3,1,2]
<strong>Output:</strong> [0,0,0]
<strong>Explanation:</strong> The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
- Node with value 3 does not have any cousins so its sum is 0.
- Node with value 1 does not have any cousins so its sum is 0.
- Node with value 2 does not have any cousins so its sum is 0.
</pre>

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

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
	<li><code>1 &lt;= Node.val &lt;= 10<sup>4</sup></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
from collections import deque, defaultdict
class Solution:
    def replaceValueInTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        '''
        thinking breadth first search with hashmap
        '''
        def bfs(root):
            def collectReplace(queue, updated_vals):
                new_queue = deque()
                # nodes = defaultdict(list)
                next_vals = deque()
                # next_vals = dict()
                level_sum = 0
                while queue:
                    cur = queue.popleft()
                    child_sum = 0
                    if cur.left:
                        new_queue.append(cur.left)
                        child_sum += cur.left.val
                        level_sum += cur.left.val
                        # nodes[cur.val].append(cur.left.val)
                    if cur.right:
                        new_queue.append(cur.right)
                        child_sum += cur.right.val
                        level_sum += cur.right.val
                        # nodes[cur.val].append(cur.right.val)
                    if cur.left: next_vals.append(child_sum)
                    if cur.right: next_vals.append(child_sum)
                    # if cur.left:
                    #     next_vals.append(child_sum)
                    # if cur.right:
                    #     next_vals.append(child_sum)
                    # if updated_vals:
                    cur.val = updated_vals.popleft()
                        # cur.val = updated_vals[cur.val]
                    # else:
                        # cur.val = 0
                for i in range(len(next_vals)):
                    # print('val, level_sum : ', '({},{})'.format(val, level_sum))
                    # val = level_sum - val
                    next_vals.append(level_sum - next_vals.popleft())
                print(next_vals)
                # if len(nodes.keys()) > 1:
                #     for key_i, value_i in nodes.items():
                #         for val in value_i:
                #             next_vals[val] = sum([sum(nodes[key_j]) for key_j in nodes.keys()]) - sum(nodes[key_i])
                return new_queue, next_vals
            
            even_queue = deque()
            odd_queue = deque()
            even_queue.append(root)
            new_vals_even = deque([0])

            while even_queue or odd_queue:
                while even_queue:
                    odd_queue, new_vals_odd = collectReplace(even_queue, new_vals_even)
                    # print('odd_queue: ', odd_queue)
                    # print('new_vals_odd: ', new_vals_odd)
                while odd_queue:
                    even_queue, new_vals_even = collectReplace(odd_queue, new_vals_odd)
                    # print('even_queue: ', even_queue)
                    # print('new_vals_even: ', new_vals_even)

        bfs(root)
        
        return root
695