Skip to content

Commit 511b0cc

Browse files
authored
feat: add js solution to lc problem: No.2196 (doocs#3275)
1 parent 31386dc commit 511b0cc

File tree

4 files changed

+163
-78
lines changed

4 files changed

+163
-78
lines changed

solution/2100-2199/2196.Create Binary Tree From Descriptions/README.md

+56-26
Original file line numberDiff line numberDiff line change
@@ -246,37 +246,67 @@ func createBinaryTree(descriptions [][]int) *TreeNode {
246246
*/
247247

248248
function createBinaryTree(descriptions: number[][]): TreeNode | null {
249-
const map = new Map<number, [number, number]>();
250-
const isRoot = new Map<number, boolean>();
251-
for (const [parent, child, isLeft] of descriptions) {
252-
let [left, right] = map.get(parent) ?? [0, 0];
253-
if (isLeft) {
254-
left = child;
255-
} else {
256-
right = child;
257-
}
258-
if (!isRoot.has(parent)) {
259-
isRoot.set(parent, true);
260-
}
261-
isRoot.set(child, false);
262-
map.set(parent, [left, right]);
249+
const nodes: Record<number, TreeNode> = {};
250+
const children = new Set<number>();
251+
252+
for (const [parent, child] of descriptions) {
253+
if (!nodes[parent]) nodes[parent] = new TreeNode(parent);
254+
if (!nodes[child]) nodes[child] = new TreeNode(child);
255+
256+
children.add(child);
263257
}
264-
const dfs = (val: number) => {
265-
if (val === 0) {
266-
return null;
267-
}
268-
const [left, right] = map.get(val) ?? [0, 0];
269-
return new TreeNode(val, dfs(left), dfs(right));
270-
};
271-
for (const [key, val] of isRoot.entries()) {
272-
if (val) {
273-
return dfs(key);
274-
}
258+
259+
let root = -1;
260+
for (const [parent, child, isLeft] of descriptions) {
261+
if (!children.has(parent)) root = parent;
262+
263+
if (isLeft) nodes[parent].left = nodes[child];
264+
else nodes[parent].right = nodes[child];
275265
}
276-
return null;
266+
267+
return nodes[root];
277268
}
278269
```
279270

271+
#### JavaScript
272+
273+
```js
274+
/**
275+
* Definition for a binary tree node.
276+
* function TreeNode(val, left, right) {
277+
* this.val = (val===undefined ? 0 : val)
278+
* this.left = (left===undefined ? null : left)
279+
* this.right = (right===undefined ? null : right)
280+
* }
281+
*/
282+
/**
283+
* @param {number[][]} descriptions
284+
* @return {TreeNode}
285+
*/
286+
287+
var createBinaryTree = function (descriptions) {
288+
const nodes = {};
289+
const children = new Set();
290+
291+
for (const [parent, child] of descriptions) {
292+
if (!nodes[parent]) nodes[parent] = new TreeNode(parent);
293+
if (!nodes[child]) nodes[child] = new TreeNode(child);
294+
295+
children.add(child);
296+
}
297+
298+
let root = -1;
299+
for (const [parent, child, isLeft] of descriptions) {
300+
if (!children.has(parent)) root = parent;
301+
302+
if (isLeft) nodes[parent].left = nodes[child];
303+
else nodes[parent].right = nodes[child];
304+
}
305+
306+
return nodes[root];
307+
};
308+
```
309+
280310
#### Rust
281311

282312
```rust

solution/2100-2199/2196.Create Binary Tree From Descriptions/README_EN.md

+56-26
Original file line numberDiff line numberDiff line change
@@ -241,37 +241,67 @@ func createBinaryTree(descriptions [][]int) *TreeNode {
241241
*/
242242

243243
function createBinaryTree(descriptions: number[][]): TreeNode | null {
244-
const map = new Map<number, [number, number]>();
245-
const isRoot = new Map<number, boolean>();
246-
for (const [parent, child, isLeft] of descriptions) {
247-
let [left, right] = map.get(parent) ?? [0, 0];
248-
if (isLeft) {
249-
left = child;
250-
} else {
251-
right = child;
252-
}
253-
if (!isRoot.has(parent)) {
254-
isRoot.set(parent, true);
255-
}
256-
isRoot.set(child, false);
257-
map.set(parent, [left, right]);
244+
const nodes: Record<number, TreeNode> = {};
245+
const children = new Set<number>();
246+
247+
for (const [parent, child] of descriptions) {
248+
if (!nodes[parent]) nodes[parent] = new TreeNode(parent);
249+
if (!nodes[child]) nodes[child] = new TreeNode(child);
250+
251+
children.add(child);
258252
}
259-
const dfs = (val: number) => {
260-
if (val === 0) {
261-
return null;
262-
}
263-
const [left, right] = map.get(val) ?? [0, 0];
264-
return new TreeNode(val, dfs(left), dfs(right));
265-
};
266-
for (const [key, val] of isRoot.entries()) {
267-
if (val) {
268-
return dfs(key);
269-
}
253+
254+
let root = -1;
255+
for (const [parent, child, isLeft] of descriptions) {
256+
if (!children.has(parent)) root = parent;
257+
258+
if (isLeft) nodes[parent].left = nodes[child];
259+
else nodes[parent].right = nodes[child];
270260
}
271-
return null;
261+
262+
return nodes[root];
272263
}
273264
```
274265

266+
#### JavaScript
267+
268+
```js
269+
/**
270+
* Definition for a binary tree node.
271+
* function TreeNode(val, left, right) {
272+
* this.val = (val===undefined ? 0 : val)
273+
* this.left = (left===undefined ? null : left)
274+
* this.right = (right===undefined ? null : right)
275+
* }
276+
*/
277+
/**
278+
* @param {number[][]} descriptions
279+
* @return {TreeNode}
280+
*/
281+
282+
var createBinaryTree = function (descriptions) {
283+
const nodes = {};
284+
const children = new Set();
285+
286+
for (const [parent, child] of descriptions) {
287+
if (!nodes[parent]) nodes[parent] = new TreeNode(parent);
288+
if (!nodes[child]) nodes[child] = new TreeNode(child);
289+
290+
children.add(child);
291+
}
292+
293+
let root = -1;
294+
for (const [parent, child, isLeft] of descriptions) {
295+
if (!children.has(parent)) root = parent;
296+
297+
if (isLeft) nodes[parent].left = nodes[child];
298+
else nodes[parent].right = nodes[child];
299+
}
300+
301+
return nodes[root];
302+
};
303+
```
304+
275305
#### Rust
276306

277307
```rust
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
* @param {number[][]} descriptions
11+
* @return {TreeNode}
12+
*/
13+
14+
var createBinaryTree = function (descriptions) {
15+
const nodes = {};
16+
const children = new Set();
17+
18+
for (const [parent, child] of descriptions) {
19+
if (!nodes[parent]) nodes[parent] = new TreeNode(parent);
20+
if (!nodes[child]) nodes[child] = new TreeNode(child);
21+
22+
children.add(child);
23+
}
24+
25+
let root = -1;
26+
for (const [parent, child, isLeft] of descriptions) {
27+
if (!children.has(parent)) root = parent;
28+
29+
if (isLeft) nodes[parent].left = nodes[child];
30+
else nodes[parent].right = nodes[child];
31+
}
32+
33+
return nodes[root];
34+
};

solution/2100-2199/2196.Create Binary Tree From Descriptions/Solution.ts

+17-26
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,23 @@
1313
*/
1414

1515
function createBinaryTree(descriptions: number[][]): TreeNode | null {
16-
const map = new Map<number, [number, number]>();
17-
const isRoot = new Map<number, boolean>();
18-
for (const [parent, child, isLeft] of descriptions) {
19-
let [left, right] = map.get(parent) ?? [0, 0];
20-
if (isLeft) {
21-
left = child;
22-
} else {
23-
right = child;
24-
}
25-
if (!isRoot.has(parent)) {
26-
isRoot.set(parent, true);
27-
}
28-
isRoot.set(child, false);
29-
map.set(parent, [left, right]);
16+
const nodes: Record<number, TreeNode> = {};
17+
const children = new Set<number>();
18+
19+
for (const [parent, child] of descriptions) {
20+
if (!nodes[parent]) nodes[parent] = new TreeNode(parent);
21+
if (!nodes[child]) nodes[child] = new TreeNode(child);
22+
23+
children.add(child);
3024
}
31-
const dfs = (val: number) => {
32-
if (val === 0) {
33-
return null;
34-
}
35-
const [left, right] = map.get(val) ?? [0, 0];
36-
return new TreeNode(val, dfs(left), dfs(right));
37-
};
38-
for (const [key, val] of isRoot.entries()) {
39-
if (val) {
40-
return dfs(key);
41-
}
25+
26+
let root = -1;
27+
for (const [parent, child, isLeft] of descriptions) {
28+
if (!children.has(parent)) root = parent;
29+
30+
if (isLeft) nodes[parent].left = nodes[child];
31+
else nodes[parent].right = nodes[child];
4232
}
43-
return null;
33+
34+
return nodes[root];
4435
}

0 commit comments

Comments
 (0)