diff --git a/solution/2600-2699/2641.Cousins in Binary Tree II/README.md b/solution/2600-2699/2641.Cousins in Binary Tree II/README.md index c47f0afe07bc7..1fa10800f4826 100644 --- a/solution/2600-2699/2641.Cousins in Binary Tree II/README.md +++ b/solution/2600-2699/2641.Cousins in Binary Tree II/README.md @@ -554,32 +554,66 @@ func replaceValueInTree(root *TreeNode) *TreeNode { */ function replaceValueInTree(root: TreeNode | null): TreeNode | null { - root.val = 0; - const q: TreeNode[] = [root]; - while (q.length > 0) { - const t: TreeNode[] = []; - let s = 0; - for (const { left, right } of q) { - if (left) { - t.push(left); - s += left.val; + let q = [root]; + let [sum, nextSum] = [0, root.val]; + + while (q.length) { + const qNext: TreeNode[] = []; + [sum, nextSum] = [nextSum, 0]; + + for (const node of q) { + const x = (node.left?.val ?? 0) + (node.right?.val ?? 0); + node.val = sum - node.val; + nextSum += x; + + if (node.left) { + node.left.val = x; + qNext.push(node.left); } - if (right) { - t.push(right); - s += right.val; + + if (node.right) { + node.right.val = x; + qNext.push(node.right); } } - for (const { left, right } of q) { - const sub = (left?.val || 0) + (right?.val || 0); - if (left) { - left.val = s - sub; + + q = qNext; + } + + return root; +} +``` + +#### JavaScript + +```js +function replaceValueInTree(root) { + let q = [root]; + let [sum, nextSum] = [0, root.val]; + + while (q.length) { + const qNext = []; + [sum, nextSum] = [nextSum, 0]; + + for (const node of q) { + const x = (node.left?.val ?? 0) + (node.right?.val ?? 0); + node.val = sum - node.val; + nextSum += x; + + if (node.left) { + node.left.val = x; + qNext.push(node.left); } - if (right) { - right.val = s - sub; + + if (node.right) { + node.right.val = x; + qNext.push(node.right); } } - q.splice(0, q.length, ...t); + + q = qNext; } + return root; } ``` diff --git a/solution/2600-2699/2641.Cousins in Binary Tree II/README_EN.md b/solution/2600-2699/2641.Cousins in Binary Tree II/README_EN.md index 2b0f516247a68..3c29eff4e22eb 100644 --- a/solution/2600-2699/2641.Cousins in Binary Tree II/README_EN.md +++ b/solution/2600-2699/2641.Cousins in Binary Tree II/README_EN.md @@ -548,32 +548,66 @@ func replaceValueInTree(root *TreeNode) *TreeNode { */ function replaceValueInTree(root: TreeNode | null): TreeNode | null { - root.val = 0; - const q: TreeNode[] = [root]; - while (q.length > 0) { - const t: TreeNode[] = []; - let s = 0; - for (const { left, right } of q) { - if (left) { - t.push(left); - s += left.val; + let q = [root]; + let [sum, nextSum] = [0, root.val]; + + while (q.length) { + const qNext: TreeNode[] = []; + [sum, nextSum] = [nextSum, 0]; + + for (const node of q) { + const x = (node.left?.val ?? 0) + (node.right?.val ?? 0); + node.val = sum - node.val; + nextSum += x; + + if (node.left) { + node.left.val = x; + qNext.push(node.left); } - if (right) { - t.push(right); - s += right.val; + + if (node.right) { + node.right.val = x; + qNext.push(node.right); } } - for (const { left, right } of q) { - const sub = (left?.val || 0) + (right?.val || 0); - if (left) { - left.val = s - sub; + + q = qNext; + } + + return root; +} +``` + +#### JavaScript + +```js +function replaceValueInTree(root) { + let q = [root]; + let [sum, nextSum] = [0, root.val]; + + while (q.length) { + const qNext = []; + [sum, nextSum] = [nextSum, 0]; + + for (const node of q) { + const x = (node.left?.val ?? 0) + (node.right?.val ?? 0); + node.val = sum - node.val; + nextSum += x; + + if (node.left) { + node.left.val = x; + qNext.push(node.left); } - if (right) { - right.val = s - sub; + + if (node.right) { + node.right.val = x; + qNext.push(node.right); } } - q.splice(0, q.length, ...t); + + q = qNext; } + return root; } ``` diff --git a/solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.js b/solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.js new file mode 100644 index 0000000000000..48d76e08e06ac --- /dev/null +++ b/solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.js @@ -0,0 +1,29 @@ +function replaceValueInTree(root) { + let q = [root]; + let [sum, nextSum] = [0, root.val]; + + while (q.length) { + const qNext = []; + [sum, nextSum] = [nextSum, 0]; + + for (const node of q) { + const x = (node.left?.val ?? 0) + (node.right?.val ?? 0); + node.val = sum - node.val; + nextSum += x; + + if (node.left) { + node.left.val = x; + qNext.push(node.left); + } + + if (node.right) { + node.right.val = x; + qNext.push(node.right); + } + } + + q = qNext; + } + + return root; +} diff --git a/solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.ts b/solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.ts index e110cb32b2db3..4191d2783fa74 100644 --- a/solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.ts +++ b/solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.ts @@ -13,31 +13,31 @@ */ function replaceValueInTree(root: TreeNode | null): TreeNode | null { - root.val = 0; - const q: TreeNode[] = [root]; - while (q.length > 0) { - const t: TreeNode[] = []; - let s = 0; - for (const { left, right } of q) { - if (left) { - t.push(left); - s += left.val; - } - if (right) { - t.push(right); - s += right.val; - } - } - for (const { left, right } of q) { - const sub = (left?.val || 0) + (right?.val || 0); - if (left) { - left.val = s - sub; + let q = [root]; + let [sum, nextSum] = [0, root.val]; + + while (q.length) { + const qNext: TreeNode[] = []; + [sum, nextSum] = [nextSum, 0]; + + for (const node of q) { + const x = (node.left?.val ?? 0) + (node.right?.val ?? 0); + node.val = sum - node.val; + nextSum += x; + + if (node.left) { + node.left.val = x; + qNext.push(node.left); } - if (right) { - right.val = s - sub; + + if (node.right) { + node.right.val = x; + qNext.push(node.right); } } - q.splice(0, q.length, ...t); + + q = qNext; } + return root; }