Given the root of a binary tree, replace the value of each node in the tree with the sum of all its cousins' values.
Two nodes of a binary tree are cousins if they have the same depth with different parents.
Return the root of the modified tree.
Note that the depth of a node is the number of edges in the path from the root node to it.
Example 1:
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 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.
Example 2:
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 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.
Constraints:
[1, 105].1 <= Node.val <= 104/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode replaceValueInTree(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int levelCount = 1, levelSum = root.val;
while (!q.isEmpty()) {
int nextLevelSum = 0;
for (int i = 0; i < levelCount; i++) {
TreeNode front = q.poll();
front.val = levelSum - front.val;
int siblingSum = (front.left != null ? front.left.val : 0)
+ (front.right != null ? front.right.val : 0);
nextLevelSum += siblingSum;
if (front.left != null) {
front.left.val = siblingSum;
q.offer(front.left);
}
if (front.right != null) {
front.right.val = siblingSum;
q.offer(front.right);
}
}
levelSum = nextLevelSum;
levelCount = q.size();
}
return root;
}
}/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode replaceValueInTree(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
ArrayList<Integer> levelSums = new ArrayList<>();
q.offer(root);
int levelCount = 1;
while (!q.isEmpty()) {
int levelSum = 0;
for (int i = 0; i < levelCount; i++) {
TreeNode front = q.poll();
levelSum += front.val;
if (front.left != null)
q.offer(front.left);
if (front.right != null)
q.offer(front.right);
}
levelSums.add(levelSum);
levelCount = q.size();
}
// System.out.println(levelSums);
modify(root, 0, root.val, levelSums);
return root;
}
private void modify(TreeNode root, int depth, int siblingsSum, List<Integer> levelSums) {
if (root == null)
return;
root.val = levelSums.get(depth) - siblingsSum;
int childrenSum = (root.left != null ? root.left.val : 0) + (root.right != null ? root.right.val : 0);
modify(root.left, depth + 1, childrenSum, levelSums);
modify(root.right, depth + 1, childrenSum, levelSums);
}
}/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode replaceValueInTree(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int levelCount = 1, levelSum = root.val;
while (!q.isEmpty()) {
int nextLevelSum = 0;
for (int i = 0; i < levelCount; i++) {
TreeNode front = q.poll();
front.val = levelSum - front.val;
int siblingSum = (front.left != null ? front.left.val : 0)
+ (front.right != null ? front.right.val : 0);
nextLevelSum += siblingSum;
if (front.left != null) {
front.left.val = siblingSum;
q.offer(front.left);
}
if (front.right != null) {
front.right.val = siblingSum;
q.offer(front.right);
}
}
levelSum = nextLevelSum;
levelCount = q.size();
}
return root;
}
}