Super easy java solution


  • 0
    Z

    public class BSTIterator {

    Queue<Integer> q;
    
    public BSTIterator(TreeNode root) {
        q = new LinkedList<>();
        
        traverse(root , q);
    }
    
    public void traverse(TreeNode root , Queue<Integer> q) {
        if(root == null) {
            return;
        }
        
        traverse(root.left , q);
        q.add(root.val);
        traverse(root.right , q);
    }
    
    /** @return whether we have a next smallest number */
    public boolean hasNext() {
        return !q.isEmpty();
    }
    
    /** @return the next smallest number */
    public int next() {
        return q.poll();
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.