c++ queue solution


  • 0
    R

    class BSTIterator {
    public:
    BSTIterator(TreeNode *root) {

        inorder(root);
    }
    
    /** @return whether we have a next smallest number */
    bool hasNext() {
        return _q.size()>0;
    }
    
    /** @return the next smallest number */
    int next() {
        int a=_q.front();
        _q.pop();
        return a;
    }
    
    void inorder(TreeNode* node)
    {
        if(node == NULL)return;
        inorder(node->left);
        _q.push(node->val);
        inorder(node->right);
    }
    queue<int> _q;
    

    };


Log in to reply
 

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