1 public class BSTIterator {
2 Queue<Integer> queue;
3 public void build(TreeNode root){
4 if(root == null) return;
5 build(root.left);
6 queue.offer(root.val);
7 build(root.right);
8
9 }
10
11 public BSTIterator(TreeNode root) {
12 queue = new LinkedList<>();
13 build(root);
14
15 }
16
17 /** @return whether we have a next smallest number */
18 public boolean hasNext() {
19 return !queue.isEmpty();
20 }
21
22 /** @return the next smallest number */
23 public int next() {
24 return queue.poll();
25 }
26 }
27
28 /**
29 * Your BSTIterator will be called like this:
30 * BSTIterator i = new BSTIterator(root);
31 * while (i.hasNext()) v[f()] = i.next();
32 */