Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Binary Search Tree Iterator problem solution

YASH PAL, 31 July 202419 January 2026

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

Binary Search Tree Iterator 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

Binary Search Tree Iterator 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);
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes