-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_2641.java
66 lines (64 loc) · 2.73 KB
/
_2641.java
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.fishercoder.solutions.thirdthousand;
import com.fishercoder.common.classes.TreeNode;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
public class _2641 {
public static class Solution1 {
/*
* My completely original solution.
* Note: It's not really replacing the values in the original tree nodes, instead, I'm building a new tree with updated values.
*/
public TreeNode replaceValueInTree(TreeNode root) {
Map<Integer, Integer> depthToLevelSumMap = new HashMap<>();
Queue<TreeNode> originalQ = new LinkedList<>();
originalQ.offer(root);
int depth = 0;
while (!originalQ.isEmpty()) {
int size = originalQ.size();
int levelSum = 0;
for (int i = 0; i < size; i++) {
TreeNode curr = originalQ.poll();
levelSum += curr.val;
if (curr.left != null) {
originalQ.offer(curr.left);
}
if (curr.right != null) {
originalQ.offer(curr.right);
}
}
depthToLevelSumMap.put(depth++, levelSum);
}
depth = 0;
TreeNode newRoot = new TreeNode(0);
originalQ.offer(root);
Queue<TreeNode> newQ = new LinkedList<>();
newQ.add(newRoot);
while (!originalQ.isEmpty()) {
int size = originalQ.size();
for (int i = 0; i < size; i++) {
TreeNode currOriginal = originalQ.poll();
int childrenSum = currOriginal.left != null ? currOriginal.left.val : 0;
childrenSum += currOriginal.right != null ? currOriginal.right.val : 0;
int remainder = depthToLevelSumMap.getOrDefault(depth + 1, 0) - childrenSum;
TreeNode currNew = newQ.poll();
if (currOriginal.left != null) {
TreeNode currNewLeftChild = new TreeNode(remainder);
currNew.left = currNewLeftChild;
newQ.offer(currNewLeftChild);
originalQ.offer(currOriginal.left);
}
if (currOriginal.right != null) {
TreeNode currNewRightChild = new TreeNode(remainder);
currNew.right = currNewRightChild;
newQ.offer(currNewRightChild);
originalQ.offer(currOriginal.right);
}
}
depth++;
}
return newRoot;
}
}
}