Skip to content

feat: add js solution to lc problem: No.1110 #3284

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 4 commits into from
Jul 19, 2024
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
174 changes: 174 additions & 0 deletions solution/1100-1199/1110.Delete Nodes And Return Forest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,180 @@ function delNodes(root: TreeNode | null, to_delete: number[]): Array<TreeNode |
}
```

#### 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)
* }
*/
/**
* @param {TreeNode} root
* @param {number[]} to_delete
* @return {TreeNode[]}
*/
var delNodes = function (root, to_delete) {
const s = Array(1001).fill(false);
for (const x of to_delete) {
s[x] = true;
}
const ans = [];
const dfs = root => {
if (!root) {
return null;
}
root.left = dfs(root.left);
root.right = dfs(root.right);
if (!s[root.val]) {
return root;
}
if (root.left) {
ans.push(root.left);
}
if (root.right) {
ans.push(root.right);
}
return null;
};
if (dfs(root)) {
ans.push(root);
}
return ans;
};
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二:BFS

<!-- tabs:start -->

#### TypeScript

```ts
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
export function delNodes(root: T, to_delete: number[]): Array<T> {
if (!root) return [];

const del = new Set(to_delete);
const res: T[] = [];
let q: TreeNode[] = [root];

while (q.length) {
const qNext: TreeNode[] = [];

for (const node of q) {
if (node.left) {
qNext.push(node.left);

if (del.has(node.left.val)) {
node.left = null;
}
}

if (node.right) {
qNext.push(node.right);

if (del.has(node.right.val)) {
node.right = null;
}
}

if (del.has(node.val)) {
if (node.left) res.push(node.left);
if (node.right) res.push(node.right);
}
}

q = qNext;
}

if (!del.has(root.val)) res.push(root);

return res;
}

type T = TreeNode | null;
```

#### 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)
* }
*/
/**
* @param {TreeNode} root
* @param {number[]} to_delete
* @return {TreeNode[]}
*/
var delNodes = function (root, to_delete) {
if (!root) return [];

const del = new Set(to_delete);
const res = [];
let q = [root];

while (q.length) {
const qNext = [];

for (const node of q) {
if (node.left) {
qNext.push(node.left);

if (del.has(node.left.val)) {
node.left = null;
}
}

if (node.right) {
qNext.push(node.right);

if (del.has(node.right.val)) {
node.right = null;
}
}

if (del.has(node.val)) {
if (node.left) res.push(node.left);
if (node.right) res.push(node.right);
}
}

q = qNext;
}

if (!del.has(root.val)) res.push(root);

return res;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
174 changes: 174 additions & 0 deletions solution/1100-1199/1110.Delete Nodes And Return Forest/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,180 @@ function delNodes(root: TreeNode | null, to_delete: number[]): Array<TreeNode |
}
```

#### 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)
* }
*/
/**
* @param {TreeNode} root
* @param {number[]} to_delete
* @return {TreeNode[]}
*/
var delNodes = function (root, to_delete) {
const s = Array(1001).fill(false);
for (const x of to_delete) {
s[x] = true;
}
const ans = [];
const dfs = root => {
if (!root) {
return null;
}
root.left = dfs(root.left);
root.right = dfs(root.right);
if (!s[root.val]) {
return root;
}
if (root.left) {
ans.push(root.left);
}
if (root.right) {
ans.push(root.right);
}
return null;
};
if (dfs(root)) {
ans.push(root);
}
return ans;
};
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: BFS

<!-- tabs:start -->

#### TypeScript

```ts
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
export function delNodes(root: T, to_delete: number[]): Array<T> {
if (!root) return [];

const del = new Set(to_delete);
const res: T[] = [];
let q: TreeNode[] = [root];

while (q.length) {
const qNext: TreeNode[] = [];

for (const node of q) {
if (node.left) {
qNext.push(node.left);

if (del.has(node.left.val)) {
node.left = null;
}
}

if (node.right) {
qNext.push(node.right);

if (del.has(node.right.val)) {
node.right = null;
}
}

if (del.has(node.val)) {
if (node.left) res.push(node.left);
if (node.right) res.push(node.right);
}
}

q = qNext;
}

if (!del.has(root.val)) res.push(root);

return res;
}

type T = TreeNode | null;
```

#### 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)
* }
*/
/**
* @param {TreeNode} root
* @param {number[]} to_delete
* @return {TreeNode[]}
*/
var delNodes = function (root, to_delete) {
if (!root) return [];

const del = new Set(to_delete);
const res = [];
let q = [root];

while (q.length) {
const qNext = [];

for (const node of q) {
if (node.left) {
qNext.push(node.left);

if (del.has(node.left.val)) {
node.left = null;
}
}

if (node.right) {
qNext.push(node.right);

if (del.has(node.right.val)) {
node.right = null;
}
}

if (del.has(node.val)) {
if (node.left) res.push(node.left);
if (node.right) res.push(node.right);
}
}

q = qNext;
}

if (!del.has(root.val)) res.push(root);

return res;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Loading
Loading