```
public class BSTIterator {
TreeNode bst;
int size ; //Keep track of nodes that are fetched
int num ; //Store the number of nodes
List<Integer> inorder; //Inorder traversal of a BST
public BSTIterator(TreeNode root) {
bst = root;
num =0;
size=0;
inorder = new ArrayList<>();
traverse(root);
}
public void traverse(TreeNode root){
if(root==null)
return;
traverse(root.left);
inorder.add(root.val);
num++;
traverse(root.right);
}
/** @return whether we have a next smallest number */
public boolean hasNext() {
if(size<num) return true;
return false;
}
/** @return the next smallest number */
public int next() {
return inorder.get(size++);
}
}
```