diff --git a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README.md b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README.md index b41b40aa0673f..aeb12cc1b0568 100644 --- a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README.md +++ b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README.md @@ -57,8 +57,8 @@ tags: 输出: [null,false,true] 解释: -FindElements findElements = new FindElements([-1,null,-1]); -findElements.find(1); // return False +FindElements findElements = new FindElements([-1,null,-1]); +findElements.find(1); // return False findElements.find(2); // return True

示例 2:

@@ -316,26 +316,24 @@ func (this *FindElements) Find(target int) bool { */ class FindElements { - private s: Set = new Set(); + readonly #s = new Set(); constructor(root: TreeNode | null) { root.val = 0; - const dfs = (root: TreeNode) => { - this.s.add(root.val); - if (root.left) { - root.left.val = root.val * 2 + 1; - dfs(root.left); - } - if (root.right) { - root.right.val = root.val * 2 + 2; - dfs(root.right); - } + + const dfs = (node: TreeNode | null, x = 0) => { + if (!node) return; + + this.#s.add(x); + dfs(node.left, x * 2 + 1); + dfs(node.right, x * 2 + 2); }; + dfs(root); } find(target: number): boolean { - return this.s.has(target); + return this.#s.has(target); } } @@ -346,6 +344,53 @@ class FindElements { */ ``` +#### JavaScript + +```js +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ + +const s = Symbol.for('s'); + +/** + * @param {TreeNode} root + */ +var FindElements = function (root) { + root.val = 0; + this[s] = new Set(); + + const dfs = (node, x = 0) => { + if (!node) return; + + this[s].add(x); + dfs(node.left, x * 2 + 1); + dfs(node.right, x * 2 + 2); + }; + + dfs(root); +}; + +/** + * @param {number} target + * @return {boolean} + */ +FindElements.prototype.find = function (target) { + return this[s].has(target); +}; + +/** + * Your FindElements object will be instantiated and called as such: + * var obj = new FindElements(root) + * var param_1 = obj.find(target) + */ +``` + diff --git a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README_EN.md b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README_EN.md index 8ed062fd7d793..59a00d29d1e5f 100644 --- a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README_EN.md +++ b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README_EN.md @@ -54,8 +54,8 @@ tags: Output [null,false,true] Explanation -FindElements findElements = new FindElements([-1,null,-1]); -findElements.find(1); // return False +FindElements findElements = new FindElements([-1,null,-1]); +findElements.find(1); // return False findElements.find(2); // return True

Example 2:

@@ -308,26 +308,24 @@ func (this *FindElements) Find(target int) bool { */ class FindElements { - private s: Set = new Set(); + readonly #s = new Set(); constructor(root: TreeNode | null) { root.val = 0; - const dfs = (root: TreeNode) => { - this.s.add(root.val); - if (root.left) { - root.left.val = root.val * 2 + 1; - dfs(root.left); - } - if (root.right) { - root.right.val = root.val * 2 + 2; - dfs(root.right); - } + + const dfs = (node: TreeNode | null, x = 0) => { + if (!node) return; + + this.#s.add(x); + dfs(node.left, x * 2 + 1); + dfs(node.right, x * 2 + 2); }; + dfs(root); } find(target: number): boolean { - return this.s.has(target); + return this.#s.has(target); } } @@ -338,6 +336,53 @@ class FindElements { */ ``` +#### JavaScript + +```js +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ + +const s = Symbol.for('s'); + +/** + * @param {TreeNode} root + */ +var FindElements = function (root) { + root.val = 0; + this[s] = new Set(); + + const dfs = (node, x = 0) => { + if (!node) return; + + this[s].add(x); + dfs(node.left, x * 2 + 1); + dfs(node.right, x * 2 + 2); + }; + + dfs(root); +}; + +/** + * @param {number} target + * @return {boolean} + */ +FindElements.prototype.find = function (target) { + return this[s].has(target); +}; + +/** + * Your FindElements object will be instantiated and called as such: + * var obj = new FindElements(root) + * var param_1 = obj.find(target) + */ +``` + diff --git a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.js b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.js new file mode 100644 index 0000000000000..aa02416cdf0e0 --- /dev/null +++ b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.js @@ -0,0 +1,42 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ + +const s = Symbol.for('s'); + +/** + * @param {TreeNode} root + */ +var FindElements = function (root) { + root.val = 0; + this[s] = new Set(); + + const dfs = (node, x = 0) => { + if (!node) return; + + this[s].add(x); + dfs(node.left, x * 2 + 1); + dfs(node.right, x * 2 + 2); + }; + + dfs(root); +}; + +/** + * @param {number} target + * @return {boolean} + */ +FindElements.prototype.find = function (target) { + return this[s].has(target); +}; + +/** + * Your FindElements object will be instantiated and called as such: + * var obj = new FindElements(root) + * var param_1 = obj.find(target) + */ diff --git a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.ts b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.ts index 66325c19fe0a0..a2fb78930175b 100644 --- a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.ts +++ b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.ts @@ -13,26 +13,24 @@ */ class FindElements { - private s: Set = new Set(); + readonly #s = new Set(); constructor(root: TreeNode | null) { root.val = 0; - const dfs = (root: TreeNode) => { - this.s.add(root.val); - if (root.left) { - root.left.val = root.val * 2 + 1; - dfs(root.left); - } - if (root.right) { - root.right.val = root.val * 2 + 2; - dfs(root.right); - } + + const dfs = (node: TreeNode | null, x = 0) => { + if (!node) return; + + this.#s.add(x); + dfs(node.left, x * 2 + 1); + dfs(node.right, x * 2 + 2); }; + dfs(root); } find(target: number): boolean { - return this.s.has(target); + return this.#s.has(target); } }