Skip to content

Commit

Permalink
173
Browse files Browse the repository at this point in the history
  • Loading branch information
userr2232 committed Feb 6, 2022
1 parent ca44f90 commit 018adf5
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions LeetCode/173v2.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 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 {
vector<int> q;
int it{0};

void helper(TreeNode* cur) {
if(!cur) return;
helper(cur->left);
q.emplace_back(cur->val);
helper(cur->right);
}

public:
BSTIterator(TreeNode* root) {
helper(root);
}

int next() {
return q[it++];
}

bool hasNext() {
return it < q.size();
}
};

/**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator* obj = new BSTIterator(root);
* int param_1 = obj->next();
* bool param_2 = obj->hasNext();
*/

0 comments on commit 018adf5

Please sign in to comment.