-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC0509.py
46 lines (41 loc) · 1.65 KB
/
LC0509.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution:
def replaceValueInTree(self, root):
if not root:
return root
node_queue = deque([root])
level_sums = []
# First BFS: Calculate sum of nodes at each level
while node_queue:
level_sum = 0
level_size = len(node_queue)
for _ in range(level_size):
current_node = node_queue.popleft()
level_sum += current_node.val
if current_node.left:
node_queue.append(current_node.left)
if current_node.right:
node_queue.append(current_node.right)
level_sums.append(level_sum)
# Second BFS: Update each node's value to sum of its cousins
node_queue.append(root)
level_index = 1
root.val = 0 # Root has no cousins
while node_queue:
level_size = len(node_queue)
for _ in range(level_size):
current_node = node_queue.popleft()
sibling_sum = (
current_node.left.val if current_node.left else 0
) + (current_node.right.val if current_node.right else 0)
if current_node.left:
current_node.left.val = (
level_sums[level_index] - sibling_sum
)
node_queue.append(current_node.left)
if current_node.right:
current_node.right.val = (
level_sums[level_index] - sibling_sum
)
node_queue.append(current_node.right)
level_index += 1
return root