deno.land / x / masx200_leetcode_test@10.6.5 / binary-search-tree-iterator / 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
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
class BSTIterator { #root: TreeNode | null | undefined; #stack: TreeNode[] = []; #cur: TreeNode | null | undefined; constructor(root: TreeNode | null) { this.#root = root; this.#cur = root; }
next(): number { if (!this.#root) { return -Infinity; } while (this.#cur) { this.#stack.push(this.#cur); this.#cur = this.#cur.left; } const cur = this.#stack.pop(); const ret = cur?.val; this.#cur = cur?.right;
return ret ?? Infinity; }
hasNext(): boolean { if (!this.#root) { return false; } return !!(this.#stack.length || this.#cur); }}
export default BSTIterator;
masx200_leetcode_test

Version Info

Tagged at
a year ago