Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
588 B
1
Indexable
Never
class BSTIterator { //BASED ON ITERATIVE INORDER TRAVERSAL
public:
    stack<TreeNode*> s;
    TreeNode* root;
    BSTIterator(TreeNode* roo) {
        //stack<TreeNode*> s;
        root=roo;
        pushf(root);
    }

    void pushf(TreeNode* node){
        while(node!=NULL){
            s.push(node);
            node=node->left;
        }
    }
    
    int next() { 
       
        TreeNode* top=s.top();
        s.pop();
        pushf(top->right);
        return top->val;
    }
    
    bool hasNext() {
        return !(s.empty());
        
    }
};