Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Binary Search Tree Iterator problem solution

YASH PAL, 31 July 2024

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);
}

coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions