Implement an iterator for a binary search tree that will iterate the nodes by value in ascending order.
Anonymous
public class BSTIterator { Queue q; BSTIterator(BSTNode root) { q = new LinkedList(); insertInorder(root); } public void insertInorder(BSTNode root) { if(root==null) { return; } insertInorder(root.left); q.add(root); insertInorder(root.right); } public boolean hasNext() { return q.size() > 0; } public BSTNode next() { return q.poll(); } }
Check out your Company Bowl for anonymous work chats.