public class BSTIterator {
Stack<TreeNode> stack = null;
public BSTIterator(TreeNode root) {
/** @return whether we have a next smallest number */
public boolean hasNext() {
return !stack.isEmpty() || curNode != null;
/** @return the next smallest number */
while (curNode != null) {
TreeNode temp = stack.pop();
* Your BSTIterator will be called like this:
* BSTIterator i = new BSTIterator(root);
* while (i.hasNext()) v[f()] = i.next();