Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 65783cd

Browse files
committed
feat: add ts solution to lc problem: No.0919
No.0919.Complete Binary Tree Inserter
1 parent 561351d commit 65783cd

File tree

3 files changed

+257
-0
lines changed

3 files changed

+257
-0
lines changed

‎solution/0900-0999/0919.Complete Binary Tree Inserter/README.md‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,73 @@ CBTInserter.prototype.get_root = function () {
339339
*/
340340
```
341341

342+
### **TypeScript**
343+
344+
```ts
345+
/**
346+
* Definition for a binary tree node.
347+
* class TreeNode {
348+
* val: number
349+
* left: TreeNode | null
350+
* right: TreeNode | null
351+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
352+
* this.val = (val===undefined ? 0 : val)
353+
* this.left = (left===undefined ? null : left)
354+
* this.right = (right===undefined ? null : right)
355+
* }
356+
* }
357+
*/
358+
359+
class CBTInserter {
360+
private root: TreeNode;
361+
private queue: TreeNode[];
362+
363+
constructor(root: TreeNode | null) {
364+
this.root = root;
365+
this.queue = [this.root];
366+
while (true) {
367+
if (this.queue[0].left == null) {
368+
break;
369+
}
370+
this.queue.push(this.queue[0].left);
371+
if (this.queue[0].right == null) {
372+
break;
373+
}
374+
this.queue.push(this.queue[0].right);
375+
this.queue.shift();
376+
}
377+
}
378+
379+
insert(val: number): number {
380+
if (this.queue[0].left != null && this.queue[0].right != null) {
381+
this.queue.shift();
382+
}
383+
const newNode = new TreeNode(val);
384+
this.queue.push(newNode);
385+
if (this.queue[0].left == null) {
386+
this.queue[0].left = newNode;
387+
return this.queue[0].val;
388+
}
389+
if (this.queue[0].right == null) {
390+
this.queue[0].right = newNode;
391+
return this.queue[0].val;
392+
}
393+
return 0;
394+
}
395+
396+
get_root(): TreeNode | null {
397+
return this.root;
398+
}
399+
}
400+
401+
/**
402+
* Your CBTInserter object will be instantiated and called as such:
403+
* var obj = new CBTInserter(root)
404+
* var param_1 = obj.insert(val)
405+
* var param_2 = obj.get_root()
406+
*/
407+
```
408+
342409
### **...**
343410

344411
```

‎solution/0900-0999/0919.Complete Binary Tree Inserter/README_EN.md‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,134 @@ func (this *CBTInserter) Get_root() *TreeNode {
265265
*/
266266
```
267267

268+
### **JavaScript**
269+
270+
```js
271+
/**
272+
* Definition for a binary tree node.
273+
* function TreeNode(val, left, right) {
274+
* this.val = (val===undefined ? 0 : val)
275+
* this.left = (left===undefined ? null : left)
276+
* this.right = (right===undefined ? null : right)
277+
* }
278+
*/
279+
/**
280+
* @param {TreeNode} root
281+
*/
282+
var CBTInserter = function (root) {
283+
this.tree = [];
284+
const q = [root];
285+
while (q.length) {
286+
const node = q.shift();
287+
this.tree.push(node);
288+
if (node.left) {
289+
q.push(node.left);
290+
}
291+
if (node.right) {
292+
q.push(node.right);
293+
}
294+
}
295+
};
296+
297+
/**
298+
* @param {number} val
299+
* @return {number}
300+
*/
301+
CBTInserter.prototype.insert = function (val) {
302+
const pid = (this.tree.length - 1) >> 1;
303+
const node = new TreeNode(val);
304+
this.tree.push(node);
305+
const p = this.tree[pid];
306+
if (!p.left) {
307+
p.left = node;
308+
} else {
309+
p.right = node;
310+
}
311+
return p.val;
312+
};
313+
314+
/**
315+
* @return {TreeNode}
316+
*/
317+
CBTInserter.prototype.get_root = function () {
318+
return this.tree[0];
319+
};
320+
321+
/**
322+
* Your CBTInserter object will be instantiated and called as such:
323+
* var obj = new CBTInserter(root)
324+
* var param_1 = obj.insert(val)
325+
* var param_2 = obj.get_root()
326+
*/
327+
```
328+
329+
### **TypeScript**
330+
331+
```ts
332+
/**
333+
* Definition for a binary tree node.
334+
* class TreeNode {
335+
* val: number
336+
* left: TreeNode | null
337+
* right: TreeNode | null
338+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
339+
* this.val = (val===undefined ? 0 : val)
340+
* this.left = (left===undefined ? null : left)
341+
* this.right = (right===undefined ? null : right)
342+
* }
343+
* }
344+
*/
345+
346+
class CBTInserter {
347+
private root: TreeNode;
348+
private queue: TreeNode[];
349+
350+
constructor(root: TreeNode | null) {
351+
this.root = root;
352+
this.queue = [this.root];
353+
while (true) {
354+
if (this.queue[0].left == null) {
355+
break;
356+
}
357+
this.queue.push(this.queue[0].left);
358+
if (this.queue[0].right == null) {
359+
break;
360+
}
361+
this.queue.push(this.queue[0].right);
362+
this.queue.shift();
363+
}
364+
}
365+
366+
insert(val: number): number {
367+
if (this.queue[0].left != null && this.queue[0].right != null) {
368+
this.queue.shift();
369+
}
370+
const newNode = new TreeNode(val);
371+
this.queue.push(newNode);
372+
if (this.queue[0].left == null) {
373+
this.queue[0].left = newNode;
374+
return this.queue[0].val;
375+
}
376+
if (this.queue[0].right == null) {
377+
this.queue[0].right = newNode;
378+
return this.queue[0].val;
379+
}
380+
return 0;
381+
}
382+
383+
get_root(): TreeNode | null {
384+
return this.root;
385+
}
386+
}
387+
388+
/**
389+
* Your CBTInserter object will be instantiated and called as such:
390+
* var obj = new CBTInserter(root)
391+
* var param_1 = obj.insert(val)
392+
* var param_2 = obj.get_root()
393+
*/
394+
```
395+
268396
### **...**
269397

270398
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
class CBTInserter {
16+
private root: TreeNode;
17+
private queue: TreeNode[];
18+
19+
constructor(root: TreeNode | null) {
20+
this.root = root;
21+
this.queue = [this.root];
22+
while (true) {
23+
if (this.queue[0].left == null) {
24+
break;
25+
}
26+
this.queue.push(this.queue[0].left);
27+
if (this.queue[0].right == null) {
28+
break;
29+
}
30+
this.queue.push(this.queue[0].right);
31+
this.queue.shift();
32+
}
33+
}
34+
35+
insert(val: number): number {
36+
if (this.queue[0].left != null && this.queue[0].right != null) {
37+
this.queue.shift();
38+
}
39+
const newNode = new TreeNode(val);
40+
this.queue.push(newNode);
41+
if (this.queue[0].left == null) {
42+
this.queue[0].left = newNode;
43+
return this.queue[0].val;
44+
}
45+
if (this.queue[0].right == null) {
46+
this.queue[0].right = newNode;
47+
return this.queue[0].val;
48+
}
49+
return 0;
50+
}
51+
52+
get_root(): TreeNode | null {
53+
return this.root;
54+
}
55+
}
56+
57+
/**
58+
* Your CBTInserter object will be instantiated and called as such:
59+
* var obj = new CBTInserter(root)
60+
* var param_1 = obj.insert(val)
61+
* var param_2 = obj.get_root()
62+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /