forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
binary-search-tree-iterator-ii.cpp
61 lines (53 loc) · 1.4 KB
/
binary-search-tree-iterator-ii.cpp
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
// Time: O(1), amortized
// Space: O(h)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class BSTIterator {
public:
BSTIterator(TreeNode* root) : pos_(-1) {
traverseLeft(root);
}
bool hasNext() {
return pos_ + 1 != size(vals_) || !empty(stk_);
}
int next() {
if (++pos_ == size(vals_)) {
TreeNode *node = stk_.back(); stk_.pop_back();
traverseLeft(node->right);
vals_.emplace_back(node->val);
}
return vals_[pos_];
}
bool hasPrev() {
return pos_ - 1 >= 0;
}
int prev() {
return vals_[--pos_];
}
private:
void traverseLeft(TreeNode *node) {
for (; node != nullptr; node = node->left) {
stk_.emplace_back(node);
}
}
vector<TreeNode *> stk_;
vector<int> vals_;
int pos_;
};
/**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator* obj = new BSTIterator(root);
* bool param_1 = obj->hasNext();
* int param_2 = obj->next();
* bool param_3 = obj->hasPrev();
* int param_4 = obj->prev();
*/