My Fast C++ Vector Solution


  • 0
    K

    '''
    class BSTIterator {
    private:
    vector<TreeNode*> nodes;
    int cur;
    public:
    void inOrder(TreeNode* root, vector<TreeNode*> &list){
    if(root== NULL){
    return;
    }
    inOrder(root->left,list);
    list.push_back(root);
    inOrder(root->right,list);
    }
    BSTIterator(TreeNode *root) {
    inOrder(root, nodes);
    cur = 0;
    }

    /** @return whether we have a next smallest number */
    bool hasNext() {
        return cur < nodes.size();
    }
    
    /** @return the next smallest number */
    int next() {
        cur++;
        return nodes[cur-1]->val;
    }
    

    };

    '''


Log in to reply
 

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