-
Notifications
You must be signed in to change notification settings - Fork 0
/
replaceValueInTree.java
38 lines (32 loc) · 1.11 KB
/
replaceValueInTree.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
class Solution {
public TreeNode replaceValueInTree(TreeNode root) {
dfs(new TreeNode[] {root});
root.val = 0;
return root;
}
private void dfs(TreeNode[] arr) {
if (arr.length == 0) return;
int sum = 0;
for (TreeNode node : arr) {
if (node == null) continue;
if (node.left != null) sum += node.left.val;
if (node.right != null) sum += node.right.val;
}
TreeNode[] childArr = new TreeNode[arr.length * 2];
int index = 0;
for (TreeNode node : arr) {
int curSum = 0;
if (node.left != null) curSum += node.left.val;
if (node.right != null) curSum += node.right.val;
if (node.left != null) {
node.left.val = sum - curSum;
childArr[index++] = node.left;
}
if (node.right != null) {
node.right.val = sum - curSum;
childArr[index++] = node.right;
}
}
dfs(java.util.Arrays.copyOf(childArr, index));
}
}