Untitled
unknown
plain_text
2 years ago
588 B
8
Indexable
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());
}
};Editor is loading...