class BSTIterator {

public:

TreeNode *head;

BSTIterator(TreeNode *root) {

head=root;

}

```
/** @return whether we have a next smallest number */
bool hasNext() {
if(!head)
return false;
return true;
}
/** @return the next smallest number */
int next() {
TreeNode *cur=head;
TreeNode *pre=NULL;
while(cur->left){
pre=cur;
cur=cur->left;
}
int tmp= cur->val;
if(pre)
pre->left=cur->right;
else
head=cur->right;
return tmp;
}
```

};