-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode-173-Binary_Search_Tree_Iterator.cpp
58 lines (50 loc) · 1.61 KB
/
leetcode-173-Binary_Search_Tree_Iterator.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
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
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) : _inVals(), _currIndex(0) { inOrderTraveler(root); }
int next() {
if(this->_inVals.size()<=(this->_currIndex)) return -1;
return this->_inVals[this->_currIndex++];
}
bool hasNext() {
if(this->_inVals.size()<=(this->_currIndex)) return false;
return true;
}
private:
std::vector<int> _inVals;
int _currIndex;
void inOrderTraveler(TreeNode* root){
if(!root) return;
inOrderTraveler(root->left);
this->_inVals.emplace_back(root->val);
inOrderTraveler(root->right);
}
};
int main() {
TreeNode* root = new TreeNode(7,new TreeNode(3),new TreeNode(15,new TreeNode(9),new TreeNode(20)));
BSTIterator bSTIterator = BSTIterator(root);
std::vector<std::string> optToRun = {"next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"};
std::cout << "[null";
for (auto &s : optToRun) {
if (s == "next") {
std::cout <<','<<bSTIterator.next();
} else if(s == "hasNext") {
std::cout <<','<<((bSTIterator.hasNext())?"true":"false");
} else {
continue;
}
}
std::cout<<']'<<std::endl;
return EXIT_SUCCESS;
}