Skip to content

feat: add solutions to lc problem: No.1261 #4098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ tags:
<strong>输出:</strong>
[null,false,true]
<strong>解释:</strong>
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 </pre>

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

class FindElements {
private s: Set<number> = new Set<number>();
readonly #s = new Set<number>();

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);
}
}

Expand All @@ -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)
*/
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ tags:
<strong>Output</strong>
[null,false,true]
<strong>Explanation</strong>
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 </pre>

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

class FindElements {
private s: Set<number> = new Set<number>();
readonly #s = new Set<number>();

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);
}
}

Expand All @@ -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)
*/
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
*/
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,24 @@
*/

class FindElements {
private s: Set<number> = new Set<number>();
readonly #s = new Set<number>();

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);
}
}

Expand Down