Skip to content

Commit 88526a3

Browse files
committed
feat: add js solution to lc problem: No.2196
1 parent 250e648 commit 88526a3

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,45 @@ function createBinaryTree(descriptions: number[][]): TreeNode | null {
268268
}
269269
```
270270

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+
271310
#### Rust
272311

273312
```rust

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,45 @@ function createBinaryTree(descriptions: number[][]): TreeNode | null {
263263
}
264264
```
265265

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+
266305
#### Rust
267306

268307
```rust
Lines changed: 34 additions & 0 deletions
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+
};

0 commit comments

Comments
 (0)