-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.ts
55 lines (52 loc) · 1.57 KB
/
solution.ts
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
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function replaceValueInTree (root: TreeNode | null): TreeNode | null {
if (!root) {
return root;
}
levelTravel([root, ]);
root.val = 0;
return root;
}
function levelTravel (nodes:TreeNode[]) {
const parent:TreeNode[] = [];
const nextLevelNodes:TreeNode[] = [];
let sum = 0;
for (const n of nodes) {
if (n.left) {
parent.push(n);
nextLevelNodes.push(n.left);
sum += n.left.val;
}
if (n.right) {
parent.push(n);
nextLevelNodes.push(n.right);
sum += n.right.val;
}
}
let index = 0;
while (index < nextLevelNodes.length) {
const hasNext = index + 1 < nextLevelNodes.length && parent[index] === parent[index + 1];
if (hasNext) {
const a = nextLevelNodes[index].val + nextLevelNodes[index + 1].val;
nextLevelNodes[index].val = sum - a;
nextLevelNodes[index + 1].val = sum - a;
index += 2;
} else {
nextLevelNodes[index].val = sum - nextLevelNodes[index].val;
index++;
}
}
nextLevelNodes.length && levelTravel(nextLevelNodes);
}