2641. Cousins in Binary Tree II
## 思路
BFS
每一層都用Hashtable記錄同父節點的和
然後再更新值 (= 整層總和 - 同父節點的和)
## Code
```python
class Solution:
def replaceValueInTree(self, root: Optional[TreeNode]) ->
Optional[TreeNode]:
root.val = 0
queue = deque()
if root.left:
queue.append((root, root.left))
if root.right:
queue.append((root, root.right))
while queue:
total = 0
table = defaultdict(int)
for _ in range(len(queue)):
parent, node = queue.popleft()
total += node.val
table[parent] += node.val
if node.left:
queue.append((node, node.left))
if node.right:
queue.append((node, node.right))
for parent in table:
if parent.left:
parent.left.val = total - table[parent]
if parent.right:
parent.right.val = total - table[parent]
return root
```