Sharing my 28ms AC with deque and inorder


  • 0
    A
    deque<int> iter;
        void inorderInsert(TreeNode* root){
            if(!root) return;
            inorderInsert(root->left);
            iter.push_back(root->val);
            inorderInsert(root->right);
        }
        BSTIterator(TreeNode *root) {
            if(root){
                inorderInsert(root);
            }
        }
    
        /** @return whether we have a next smallest number */
        bool hasNext() {
            return (!iter.empty());
        }
    
        /** @return the next smallest number */
        int next() {
            int ret = iter.front();
            iter.pop_front();
            return ret;
        }

  • 1
    H

    this doesn't meet the requirement of o(h) space


Log in to reply
 

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