deno.land / x / masx200_leetcode_test@10.6.5 / complete-binary-tree-inserter / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
class CBTInserter { #root: TreeNode | null; #deque: TreeNode[] = []; constructor(root: TreeNode | null) { this.#root = root; }
insert(v: number): number { if (this.#root && this.#deque.length === 0) { this.#deque.push(this.#root); for (let i = 0; i < this.#deque.length; i++) { const node = this.#deque[i];
if (node.left) { this.#deque.push(node.left); } if (node.right) { this.#deque.push(node.right); } } } const n = this.#deque.length; const parent = this.#deque[(n - 1) >> 1]; const node = new TreeNode(v); if (!parent.left) { parent.left = node; } else { parent.right = node; } this.#deque.push(node); return parent.val; }
get_root(): TreeNode | null { return this.#root; }}export default CBTInserter;
masx200_leetcode_test

Version Info

Tagged at
a year ago