Skip to content

Commit 57c8f3f

Browse files
authored
feat: add solutions to lc problem: No.1261 (#4098)
1 parent 8a4905e commit 57c8f3f

File tree

4 files changed

+170
-40
lines changed

4 files changed

+170
-40
lines changed

solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README.md

+59-14
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ tags:
5757
<strong>输出:</strong>
5858
[null,false,true]
5959
<strong>解释:</strong>
60-
FindElements findElements = new FindElements([-1,null,-1]);
61-
findElements.find(1); // return False
60+
FindElements findElements = new FindElements([-1,null,-1]);
61+
findElements.find(1); // return False
6262
findElements.find(2); // return True </pre>
6363

6464
<p><strong class="example">示例 2:</strong></p>
@@ -316,26 +316,24 @@ func (this *FindElements) Find(target int) bool {
316316
*/
317317

318318
class FindElements {
319-
private s: Set<number> = new Set<number>();
319+
readonly #s = new Set<number>();
320320

321321
constructor(root: TreeNode | null) {
322322
root.val = 0;
323-
const dfs = (root: TreeNode) => {
324-
this.s.add(root.val);
325-
if (root.left) {
326-
root.left.val = root.val * 2 + 1;
327-
dfs(root.left);
328-
}
329-
if (root.right) {
330-
root.right.val = root.val * 2 + 2;
331-
dfs(root.right);
332-
}
323+
324+
const dfs = (node: TreeNode | null, x = 0) => {
325+
if (!node) return;
326+
327+
this.#s.add(x);
328+
dfs(node.left, x * 2 + 1);
329+
dfs(node.right, x * 2 + 2);
333330
};
331+
334332
dfs(root);
335333
}
336334

337335
find(target: number): boolean {
338-
return this.s.has(target);
336+
return this.#s.has(target);
339337
}
340338
}
341339

@@ -346,6 +344,53 @@ class FindElements {
346344
*/
347345
```
348346

347+
#### JavaScript
348+
349+
```js
350+
/**
351+
* Definition for a binary tree node.
352+
* function TreeNode(val, left, right) {
353+
* this.val = (val===undefined ? 0 : val)
354+
* this.left = (left===undefined ? null : left)
355+
* this.right = (right===undefined ? null : right)
356+
* }
357+
*/
358+
359+
const s = Symbol.for('s');
360+
361+
/**
362+
* @param {TreeNode} root
363+
*/
364+
var FindElements = function (root) {
365+
root.val = 0;
366+
this[s] = new Set();
367+
368+
const dfs = (node, x = 0) => {
369+
if (!node) return;
370+
371+
this[s].add(x);
372+
dfs(node.left, x * 2 + 1);
373+
dfs(node.right, x * 2 + 2);
374+
};
375+
376+
dfs(root);
377+
};
378+
379+
/**
380+
* @param {number} target
381+
* @return {boolean}
382+
*/
383+
FindElements.prototype.find = function (target) {
384+
return this[s].has(target);
385+
};
386+
387+
/**
388+
* Your FindElements object will be instantiated and called as such:
389+
* var obj = new FindElements(root)
390+
* var param_1 = obj.find(target)
391+
*/
392+
```
393+
349394
<!-- tabs:end -->
350395

351396
<!-- solution:end -->

solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README_EN.md

+59-14
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ tags:
5454
<strong>Output</strong>
5555
[null,false,true]
5656
<strong>Explanation</strong>
57-
FindElements findElements = new FindElements([-1,null,-1]);
58-
findElements.find(1); // return False
57+
FindElements findElements = new FindElements([-1,null,-1]);
58+
findElements.find(1); // return False
5959
findElements.find(2); // return True </pre>
6060

6161
<p><strong class="example">Example 2:</strong></p>
@@ -308,26 +308,24 @@ func (this *FindElements) Find(target int) bool {
308308
*/
309309

310310
class FindElements {
311-
private s: Set<number> = new Set<number>();
311+
readonly #s = new Set<number>();
312312

313313
constructor(root: TreeNode | null) {
314314
root.val = 0;
315-
const dfs = (root: TreeNode) => {
316-
this.s.add(root.val);
317-
if (root.left) {
318-
root.left.val = root.val * 2 + 1;
319-
dfs(root.left);
320-
}
321-
if (root.right) {
322-
root.right.val = root.val * 2 + 2;
323-
dfs(root.right);
324-
}
315+
316+
const dfs = (node: TreeNode | null, x = 0) => {
317+
if (!node) return;
318+
319+
this.#s.add(x);
320+
dfs(node.left, x * 2 + 1);
321+
dfs(node.right, x * 2 + 2);
325322
};
323+
326324
dfs(root);
327325
}
328326

329327
find(target: number): boolean {
330-
return this.s.has(target);
328+
return this.#s.has(target);
331329
}
332330
}
333331

@@ -338,6 +336,53 @@ class FindElements {
338336
*/
339337
```
340338

339+
#### JavaScript
340+
341+
```js
342+
/**
343+
* Definition for a binary tree node.
344+
* function TreeNode(val, left, right) {
345+
* this.val = (val===undefined ? 0 : val)
346+
* this.left = (left===undefined ? null : left)
347+
* this.right = (right===undefined ? null : right)
348+
* }
349+
*/
350+
351+
const s = Symbol.for('s');
352+
353+
/**
354+
* @param {TreeNode} root
355+
*/
356+
var FindElements = function (root) {
357+
root.val = 0;
358+
this[s] = new Set();
359+
360+
const dfs = (node, x = 0) => {
361+
if (!node) return;
362+
363+
this[s].add(x);
364+
dfs(node.left, x * 2 + 1);
365+
dfs(node.right, x * 2 + 2);
366+
};
367+
368+
dfs(root);
369+
};
370+
371+
/**
372+
* @param {number} target
373+
* @return {boolean}
374+
*/
375+
FindElements.prototype.find = function (target) {
376+
return this[s].has(target);
377+
};
378+
379+
/**
380+
* Your FindElements object will be instantiated and called as such:
381+
* var obj = new FindElements(root)
382+
* var param_1 = obj.find(target)
383+
*/
384+
```
385+
341386
<!-- tabs:end -->
342387

343388
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
10+
const s = Symbol.for('s');
11+
12+
/**
13+
* @param {TreeNode} root
14+
*/
15+
var FindElements = function (root) {
16+
root.val = 0;
17+
this[s] = new Set();
18+
19+
const dfs = (node, x = 0) => {
20+
if (!node) return;
21+
22+
this[s].add(x);
23+
dfs(node.left, x * 2 + 1);
24+
dfs(node.right, x * 2 + 2);
25+
};
26+
27+
dfs(root);
28+
};
29+
30+
/**
31+
* @param {number} target
32+
* @return {boolean}
33+
*/
34+
FindElements.prototype.find = function (target) {
35+
return this[s].has(target);
36+
};
37+
38+
/**
39+
* Your FindElements object will be instantiated and called as such:
40+
* var obj = new FindElements(root)
41+
* var param_1 = obj.find(target)
42+
*/

solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,24 @@
1313
*/
1414

1515
class FindElements {
16-
private s: Set<number> = new Set<number>();
16+
readonly #s = new Set<number>();
1717

1818
constructor(root: TreeNode | null) {
1919
root.val = 0;
20-
const dfs = (root: TreeNode) => {
21-
this.s.add(root.val);
22-
if (root.left) {
23-
root.left.val = root.val * 2 + 1;
24-
dfs(root.left);
25-
}
26-
if (root.right) {
27-
root.right.val = root.val * 2 + 2;
28-
dfs(root.right);
29-
}
20+
21+
const dfs = (node: TreeNode | null, x = 0) => {
22+
if (!node) return;
23+
24+
this.#s.add(x);
25+
dfs(node.left, x * 2 + 1);
26+
dfs(node.right, x * 2 + 2);
3027
};
28+
3129
dfs(root);
3230
}
3331

3432
find(target: number): boolean {
35-
return this.s.has(target);
33+
return this.#s.has(target);
3634
}
3735
}
3836

0 commit comments

Comments
 (0)