graph TD;
0 --> A(0) & B(0)
A --> 7 & C(7)
B --> D("#") & 11
1
2
3
4
5
6
7
8
9
Input: root =[5,4,9,1,10,null,7]Output: [0,0,0,7,7,null,11]Explanation: 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 is0.- Node with value 4 does not have any cousins so its sum is0.- Node with value 9 does not have any cousins so its sum is0.- Node with value 1 has a cousin with value 7 so its sum is7.- Node with value 10 has a cousin with value 7 so its sum is7.- Node with value 7 has cousins with values 1 and 10 so its sum is11.
Example 2:
Input:
1
2
3
3/ \
12
Output:
1
2
3
0/ \
00
1
2
3
4
5
6
Input: root =[3,1,2]Output: [0,0,0]Explanation: 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 is0.- Node with value 1 does not have any cousins so its sum is0.- Node with value 2 does not have any cousins so its sum is0.
publicclassSolution {
public TreeNode replaceValueInTree(TreeNode root) {
// If the tree is empty, return nullif (root ==null) returnnull;
// Initialize a queue for BFS traversal Queue<TreeNode> queue =new LinkedList<>();
// List to store the sum of values at each level List<Integer> levelSums =new ArrayList<>();
// Map to track the sum of sibling values for each node Map<TreeNode, Integer> siblingSums =new HashMap<>();
// Start BFS with the root node queue.offer(root);
siblingSums.put(root, root.val);
// First BFS to calculate level sums and track sibling sumswhile (!queue.isEmpty()) {
int levelSize = queue.size(); // Number of nodes at the current levelint levelSum = 0; // Sum of node values at the current level// Traverse all nodes at the current levelfor (int i = 0; i < levelSize; i++) {
TreeNode node = queue.poll();
levelSum += node.val;
int siblingSum = 0;
// If the node has a left child, process itif (node.left!=null) {
queue.offer(node.left);
siblingSum += node.left.val;
}
// If the node has a right child, process itif (node.right!=null) {
queue.offer(node.right);
siblingSum += node.right.val;
}
// Update sibling sum for childrenif (node.left!=null) {
siblingSums.put(node.left, siblingSum);
}
if (node.right!=null) {
siblingSums.put(node.right, siblingSum);
}
}
// Store the level sum levelSums.add(levelSum);
}
// BFS to modify the tree with cousin sums queue.clear();
queue.offer(root);
int level = 0;
while (!queue.isEmpty()) {
int levelSize = queue.size(); // Number of nodes at the current levelint levelSum = levelSums.get(level); // Sum of node values at the current level// Traverse all nodes at the current levelfor (int i = 0; i < levelSize; i++) {
TreeNode node = queue.poll();
// Calculate the cousin sumint cousinSum = levelSum - siblingSums.get(node);
// Replace the node's value with the cousin sum node.val= cousinSum;
// Process the left childif (node.left!=null) {
queue.offer(node.left);
}
// Process the right childif (node.right!=null) {
queue.offer(node.right);
}
}
level++;
}
return root;
}
}