In this Leetcode Binary Search Tree Iterator problem solution we need to Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST):

BSTIterator(TreeNode root) Initializes an object of the BSTIterator class. The root of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.

boolean hasNext() Returns true if there exists a number in the traversal to the right of the pointer, otherwise returns false.

int next() Moves the pointer to the right, then returns the number at the pointer.

Notice that by initializing the pointer to a non-existent smallest number, the first call to next() will return the smallest element in the BST.

You may assume that the next() calls will always be valid. That is, there will be at least a next number in the in-order traversal when next() is called.

Leetcode Binary Search Tree Iterator problem solution


Problem solution in Python.

class BSTIterator:

    def __init__(self, root: TreeNode):
        self.stack = []
        self.root = root

    def next(self) -> int:

        while self.root:
            self.stack.append(self.root)
            self.root = self.root.left
        tep = self.stack.pop()
        res = tep.val
        self.root = tep.right
        return res
        

    def hasNext(self) -> bool:
        return len(self.stack) > 0 or self.root != None



Problem solution in Java.

public class BSTIterator {
    Stack<TreeNode> stack;

    public BSTIterator(TreeNode root) {
        stack = new Stack<>();
        getLeftMostLeaf(root, stack);
    }

    public boolean hasNext() {
        return !stack.isEmpty();
    }

    public int next() {
        TreeNode node = stack.pop();
        if (node.right != null) getLeftMostLeaf(node.right, stack);
        return node.val;
    }
    
    private TreeNode getLeftMostLeaf(TreeNode node, Stack<TreeNode> stack) {
        if (node == null) return null;
        stack.push(node);
        while (node.left != null) {
            node = node.left;
            stack.push(node);
        }
        return node;
    }
}


Problem solution in C++.

stack<TreeNode*> st;
    BSTIterator(TreeNode* root) {
        push_to_stack(root);
        
    }

    int next() {
        TreeNode *currNode = st.top();
        st.pop();
        push_to_stack(currNode->right);
        return currNode->val; 
        
    }
    
    bool hasNext() {
        return !st.empty();
        
    }
    void push_to_stack(TreeNode *node) {
        for (;node != NULL; st.push(node), node = node->left);
    }


Problem solution in C.

#define LEN 100
struct BSTIterator
{
    struct TreeNode** stack;
    int size;
};

void collectLeft(struct TreeNode* root, struct BSTIterator* t)
{
    for(struct TreeNode* l=root; l; l=l->left)
    {

        t->stack[(t->size)++] = l;
    }
}


struct BSTIterator* bstIteratorCreate(struct TreeNode* root)
{
    struct BSTIterator* t = (struct BSTIterator*)malloc(sizeof(struct BSTIterator));
    t->stack = (struct TreeNode**)malloc(sizeof(struct TreeNode*)*LEN);
    t->size = 0;
    collectLeft(root, t);
    return t;
}

bool bstIteratorHasNext(struct BSTIterator* iter)
{
    return iter->size;
}

int bstIteratorNext(struct BSTIterator* iter)
{
    int ret = iter->stack[iter->size-1]->val;
    iter->size--;
    collectLeft(iter->stack[iter->size]->right, iter);
    return ret;
}

void bstIteratorFree(struct BSTIterator* iter)
{
    free(iter->stack);
    free(iter);
}