C++ mid order


  • 0
    B

    class BSTIterator {
    public:
    queue<int> q;
    BSTIterator(TreeNode root) {
    if(root)mid_order(root);
    }
    void mid_order(TreeNode
    root)
    {
    if(root->left)mid_order(root->left);
    q.push(root->val);
    if(root->right)mid_order(root->right);
    }
    /** @return whether we have a next smallest number */
    bool hasNext() {
    return !q.empty();
    }

    /** @return the next smallest number */
    int next() {
        int i=q.front();
        q.pop();
        return i;
    }
    

    };


Log in to reply
 

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