File tree Expand file tree Collapse file tree 3 files changed +112
-0
lines changed
solution/2100-2199/2196.Create Binary Tree From Descriptions Expand file tree Collapse file tree 3 files changed +112
-0
lines changed Original file line number Diff line number Diff line change @@ -268,6 +268,45 @@ function createBinaryTree(descriptions: number[][]): TreeNode | null {
268
268
}
269
269
```
270
270
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
+
271
310
#### Rust
272
311
273
312
``` rust
Original file line number Diff line number Diff line change @@ -263,6 +263,45 @@ function createBinaryTree(descriptions: number[][]): TreeNode | null {
263
263
}
264
264
```
265
265
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
+
266
305
#### Rust
267
306
268
307
``` rust
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments