Skip to content

feat: add js solution to lc problem: No.2196 #3275

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 2 commits into from
Jul 16, 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
Original file line number Diff line number Diff line change
Expand Up @@ -246,37 +246,67 @@ func createBinaryTree(descriptions [][]int) *TreeNode {
*/

function createBinaryTree(descriptions: number[][]): TreeNode | null {
const map = new Map<number, [number, number]>();
const isRoot = new Map<number, boolean>();
for (const [parent, child, isLeft] of descriptions) {
let [left, right] = map.get(parent) ?? [0, 0];
if (isLeft) {
left = child;
} else {
right = child;
}
if (!isRoot.has(parent)) {
isRoot.set(parent, true);
}
isRoot.set(child, false);
map.set(parent, [left, right]);
const nodes: Record<number, TreeNode> = {};
const children = new Set<number>();

for (const [parent, child] of descriptions) {
if (!nodes[parent]) nodes[parent] = new TreeNode(parent);
if (!nodes[child]) nodes[child] = new TreeNode(child);

children.add(child);
}
const dfs = (val: number) => {
if (val === 0) {
return null;
}
const [left, right] = map.get(val) ?? [0, 0];
return new TreeNode(val, dfs(left), dfs(right));
};
for (const [key, val] of isRoot.entries()) {
if (val) {
return dfs(key);
}

let root = -1;
for (const [parent, child, isLeft] of descriptions) {
if (!children.has(parent)) root = parent;

if (isLeft) nodes[parent].left = nodes[child];
else nodes[parent].right = nodes[child];
}
return null;

return nodes[root];
}
```

#### 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 {number[][]} descriptions
* @return {TreeNode}
*/

var createBinaryTree = function (descriptions) {
const nodes = {};
const children = new Set();

for (const [parent, child] of descriptions) {
if (!nodes[parent]) nodes[parent] = new TreeNode(parent);
if (!nodes[child]) nodes[child] = new TreeNode(child);

children.add(child);
}

let root = -1;
for (const [parent, child, isLeft] of descriptions) {
if (!children.has(parent)) root = parent;

if (isLeft) nodes[parent].left = nodes[child];
else nodes[parent].right = nodes[child];
}

return nodes[root];
};
```

#### Rust

```rust
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,37 +241,67 @@ func createBinaryTree(descriptions [][]int) *TreeNode {
*/

function createBinaryTree(descriptions: number[][]): TreeNode | null {
const map = new Map<number, [number, number]>();
const isRoot = new Map<number, boolean>();
for (const [parent, child, isLeft] of descriptions) {
let [left, right] = map.get(parent) ?? [0, 0];
if (isLeft) {
left = child;
} else {
right = child;
}
if (!isRoot.has(parent)) {
isRoot.set(parent, true);
}
isRoot.set(child, false);
map.set(parent, [left, right]);
const nodes: Record<number, TreeNode> = {};
const children = new Set<number>();

for (const [parent, child] of descriptions) {
if (!nodes[parent]) nodes[parent] = new TreeNode(parent);
if (!nodes[child]) nodes[child] = new TreeNode(child);

children.add(child);
}
const dfs = (val: number) => {
if (val === 0) {
return null;
}
const [left, right] = map.get(val) ?? [0, 0];
return new TreeNode(val, dfs(left), dfs(right));
};
for (const [key, val] of isRoot.entries()) {
if (val) {
return dfs(key);
}

let root = -1;
for (const [parent, child, isLeft] of descriptions) {
if (!children.has(parent)) root = parent;

if (isLeft) nodes[parent].left = nodes[child];
else nodes[parent].right = nodes[child];
}
return null;

return nodes[root];
}
```

#### 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 {number[][]} descriptions
* @return {TreeNode}
*/

var createBinaryTree = function (descriptions) {
const nodes = {};
const children = new Set();

for (const [parent, child] of descriptions) {
if (!nodes[parent]) nodes[parent] = new TreeNode(parent);
if (!nodes[child]) nodes[child] = new TreeNode(child);

children.add(child);
}

let root = -1;
for (const [parent, child, isLeft] of descriptions) {
if (!children.has(parent)) root = parent;

if (isLeft) nodes[parent].left = nodes[child];
else nodes[parent].right = nodes[child];
}

return nodes[root];
};
```

#### Rust

```rust
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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 {number[][]} descriptions
* @return {TreeNode}
*/

var createBinaryTree = function (descriptions) {
const nodes = {};
const children = new Set();

for (const [parent, child] of descriptions) {
if (!nodes[parent]) nodes[parent] = new TreeNode(parent);
if (!nodes[child]) nodes[child] = new TreeNode(child);

children.add(child);
}

let root = -1;
for (const [parent, child, isLeft] of descriptions) {
if (!children.has(parent)) root = parent;

if (isLeft) nodes[parent].left = nodes[child];
else nodes[parent].right = nodes[child];
}

return nodes[root];
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,23 @@
*/

function createBinaryTree(descriptions: number[][]): TreeNode | null {
const map = new Map<number, [number, number]>();
const isRoot = new Map<number, boolean>();
for (const [parent, child, isLeft] of descriptions) {
let [left, right] = map.get(parent) ?? [0, 0];
if (isLeft) {
left = child;
} else {
right = child;
}
if (!isRoot.has(parent)) {
isRoot.set(parent, true);
}
isRoot.set(child, false);
map.set(parent, [left, right]);
const nodes: Record<number, TreeNode> = {};
const children = new Set<number>();

for (const [parent, child] of descriptions) {
if (!nodes[parent]) nodes[parent] = new TreeNode(parent);
if (!nodes[child]) nodes[child] = new TreeNode(child);

children.add(child);
}
const dfs = (val: number) => {
if (val === 0) {
return null;
}
const [left, right] = map.get(val) ?? [0, 0];
return new TreeNode(val, dfs(left), dfs(right));
};
for (const [key, val] of isRoot.entries()) {
if (val) {
return dfs(key);
}

let root = -1;
for (const [parent, child, isLeft] of descriptions) {
if (!children.has(parent)) root = parent;

if (isLeft) nodes[parent].left = nodes[child];
else nodes[parent].right = nodes[child];
}
return null;

return nodes[root];
}
Loading