-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-search-tree-iterator.js
More file actions
66 lines (55 loc) · 1.42 KB
/
binary-search-tree-iterator.js
File metadata and controls
66 lines (55 loc) · 1.42 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Problem: Binary Search Tree Iterator
* Link: https://leetcode.com/problems/binary-search-tree-iterator/
* Difficulty: Medium
*
* Implement an iterator over a BST with next() and hasNext().
* next() returns the next smallest number.
*
* Time Complexity: O(1) average per next() call
* Space Complexity: O(h) for the stack
*/
// JavaScript Solution - Controlled Inorder using Stack
class BSTIterator {
constructor(root) {
this.stack = [];
this._pushLeft(root); // initialize with leftmost path
}
// Push all left children onto stack
_pushLeft(node) {
while (node) {
this.stack.push(node);
node = node.left;
}
}
// Return the next smallest number
next() {
const node = this.stack.pop();
if (node.right) {
this._pushLeft(node.right); // process right subtree
}
return node.val;
}
// Return whether we have a next smallest number
hasNext() {
return this.stack.length > 0;
}
}
module.exports = BSTIterator;
/* Python Solution:
class BSTIterator:
def __init__(self, root):
self.stack = []
self._push_left(root)
def _push_left(self, node):
while node:
self.stack.append(node)
node = node.left
def next(self):
node = self.stack.pop()
if node.right:
self._push_left(node.right)
return node.val
def hasNext(self):
return len(self.stack) > 0
*/