Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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:
- stack<TreeNode *> s;
- TreeNode *curr;
- BSTIterator(TreeNode* root) {
- curr = root;
- }
- int next() {
- while(curr){
- s.push(curr);
- curr = curr->left;
- }
- TreeNode *tmp = s.top();
- s.pop();
- curr = tmp->right;
- return tmp->val;
- }
- bool hasNext() {
- return curr || !s.empty();
- }
- };
- /**
- * 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();
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement