Java. Why does Deque have Memory Limit Exceed? But Stack works.


  • 0
    F
        public class BSTIterator {
        private TreeNode node;
        private Deque<TreeNode> aStack = new ArrayDeque<TreeNode>();
        
        public BSTIterator(TreeNode root) {
            node = root;
        }
    
        /** @return whether we have a next smallest number */
        public boolean hasNext() {
            if(aStack.isEmpty() && node == null) return false;
            else return true;
        }
    
        /** @return the next smallest number */
        public int next() {
            while(node != null)
            {
                aStack.push(node);
                node = node.left;
            }
            node = aStack.peekLast();
            aStack.pop();
            int ret = node.val;
            node = node.right;
            return ret;
        }
        
        public class TreeNode {
             int val;
             TreeNode left;
             TreeNode right;
             TreeNode(int x) { val = x; }
         }
    }

Log in to reply
 

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