Skip to content

Commit 9850a67

Browse files
authored
feat: update solutions to lc problem: No.2641 (#3665)
1 parent 8777c7f commit 9850a67

File tree

4 files changed

+157
-60
lines changed

4 files changed

+157
-60
lines changed

solution/2600-2699/2641.Cousins in Binary Tree II/README.md

+53-19
Original file line numberDiff line numberDiff line change
@@ -554,32 +554,66 @@ func replaceValueInTree(root *TreeNode) *TreeNode {
554554
*/
555555

556556
function replaceValueInTree(root: TreeNode | null): TreeNode | null {
557-
root.val = 0;
558-
const q: TreeNode[] = [root];
559-
while (q.length > 0) {
560-
const t: TreeNode[] = [];
561-
let s = 0;
562-
for (const { left, right } of q) {
563-
if (left) {
564-
t.push(left);
565-
s += left.val;
557+
let q = [root];
558+
let [sum, nextSum] = [0, root.val];
559+
560+
while (q.length) {
561+
const qNext: TreeNode[] = [];
562+
[sum, nextSum] = [nextSum, 0];
563+
564+
for (const node of q) {
565+
const x = (node.left?.val ?? 0) + (node.right?.val ?? 0);
566+
node.val = sum - node.val;
567+
nextSum += x;
568+
569+
if (node.left) {
570+
node.left.val = x;
571+
qNext.push(node.left);
566572
}
567-
if (right) {
568-
t.push(right);
569-
s += right.val;
573+
574+
if (node.right) {
575+
node.right.val = x;
576+
qNext.push(node.right);
570577
}
571578
}
572-
for (const { left, right } of q) {
573-
const sub = (left?.val || 0) + (right?.val || 0);
574-
if (left) {
575-
left.val = s - sub;
579+
580+
q = qNext;
581+
}
582+
583+
return root;
584+
}
585+
```
586+
587+
#### JavaScript
588+
589+
```js
590+
function replaceValueInTree(root) {
591+
let q = [root];
592+
let [sum, nextSum] = [0, root.val];
593+
594+
while (q.length) {
595+
const qNext = [];
596+
[sum, nextSum] = [nextSum, 0];
597+
598+
for (const node of q) {
599+
const x = (node.left?.val ?? 0) + (node.right?.val ?? 0);
600+
node.val = sum - node.val;
601+
nextSum += x;
602+
603+
if (node.left) {
604+
node.left.val = x;
605+
qNext.push(node.left);
576606
}
577-
if (right) {
578-
right.val = s - sub;
607+
608+
if (node.right) {
609+
node.right.val = x;
610+
qNext.push(node.right);
579611
}
580612
}
581-
q.splice(0, q.length, ...t);
613+
614+
q = qNext;
582615
}
616+
583617
return root;
584618
}
585619
```

solution/2600-2699/2641.Cousins in Binary Tree II/README_EN.md

+53-19
Original file line numberDiff line numberDiff line change
@@ -548,32 +548,66 @@ func replaceValueInTree(root *TreeNode) *TreeNode {
548548
*/
549549

550550
function replaceValueInTree(root: TreeNode | null): TreeNode | null {
551-
root.val = 0;
552-
const q: TreeNode[] = [root];
553-
while (q.length > 0) {
554-
const t: TreeNode[] = [];
555-
let s = 0;
556-
for (const { left, right } of q) {
557-
if (left) {
558-
t.push(left);
559-
s += left.val;
551+
let q = [root];
552+
let [sum, nextSum] = [0, root.val];
553+
554+
while (q.length) {
555+
const qNext: TreeNode[] = [];
556+
[sum, nextSum] = [nextSum, 0];
557+
558+
for (const node of q) {
559+
const x = (node.left?.val ?? 0) + (node.right?.val ?? 0);
560+
node.val = sum - node.val;
561+
nextSum += x;
562+
563+
if (node.left) {
564+
node.left.val = x;
565+
qNext.push(node.left);
560566
}
561-
if (right) {
562-
t.push(right);
563-
s += right.val;
567+
568+
if (node.right) {
569+
node.right.val = x;
570+
qNext.push(node.right);
564571
}
565572
}
566-
for (const { left, right } of q) {
567-
const sub = (left?.val || 0) + (right?.val || 0);
568-
if (left) {
569-
left.val = s - sub;
573+
574+
q = qNext;
575+
}
576+
577+
return root;
578+
}
579+
```
580+
581+
#### JavaScript
582+
583+
```js
584+
function replaceValueInTree(root) {
585+
let q = [root];
586+
let [sum, nextSum] = [0, root.val];
587+
588+
while (q.length) {
589+
const qNext = [];
590+
[sum, nextSum] = [nextSum, 0];
591+
592+
for (const node of q) {
593+
const x = (node.left?.val ?? 0) + (node.right?.val ?? 0);
594+
node.val = sum - node.val;
595+
nextSum += x;
596+
597+
if (node.left) {
598+
node.left.val = x;
599+
qNext.push(node.left);
570600
}
571-
if (right) {
572-
right.val = s - sub;
601+
602+
if (node.right) {
603+
node.right.val = x;
604+
qNext.push(node.right);
573605
}
574606
}
575-
q.splice(0, q.length, ...t);
607+
608+
q = qNext;
576609
}
610+
577611
return root;
578612
}
579613
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function replaceValueInTree(root) {
2+
let q = [root];
3+
let [sum, nextSum] = [0, root.val];
4+
5+
while (q.length) {
6+
const qNext = [];
7+
[sum, nextSum] = [nextSum, 0];
8+
9+
for (const node of q) {
10+
const x = (node.left?.val ?? 0) + (node.right?.val ?? 0);
11+
node.val = sum - node.val;
12+
nextSum += x;
13+
14+
if (node.left) {
15+
node.left.val = x;
16+
qNext.push(node.left);
17+
}
18+
19+
if (node.right) {
20+
node.right.val = x;
21+
qNext.push(node.right);
22+
}
23+
}
24+
25+
q = qNext;
26+
}
27+
28+
return root;
29+
}

solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.ts

+22-22
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,31 @@
1313
*/
1414

1515
function replaceValueInTree(root: TreeNode | null): TreeNode | null {
16-
root.val = 0;
17-
const q: TreeNode[] = [root];
18-
while (q.length > 0) {
19-
const t: TreeNode[] = [];
20-
let s = 0;
21-
for (const { left, right } of q) {
22-
if (left) {
23-
t.push(left);
24-
s += left.val;
25-
}
26-
if (right) {
27-
t.push(right);
28-
s += right.val;
29-
}
30-
}
31-
for (const { left, right } of q) {
32-
const sub = (left?.val || 0) + (right?.val || 0);
33-
if (left) {
34-
left.val = s - sub;
16+
let q = [root];
17+
let [sum, nextSum] = [0, root.val];
18+
19+
while (q.length) {
20+
const qNext: TreeNode[] = [];
21+
[sum, nextSum] = [nextSum, 0];
22+
23+
for (const node of q) {
24+
const x = (node.left?.val ?? 0) + (node.right?.val ?? 0);
25+
node.val = sum - node.val;
26+
nextSum += x;
27+
28+
if (node.left) {
29+
node.left.val = x;
30+
qNext.push(node.left);
3531
}
36-
if (right) {
37-
right.val = s - sub;
32+
33+
if (node.right) {
34+
node.right.val = x;
35+
qNext.push(node.right);
3836
}
3937
}
40-
q.splice(0, q.length, ...t);
38+
39+
q = qNext;
4140
}
41+
4242
return root;
4343
}

0 commit comments

Comments
 (0)