Binary Search Tree Iterator LeetCode Solution

Here, We see Binary Search Tree Iterator LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Binary Search Tree Iterator LeetCode Solution

Binary Search Tree Iterator LeetCode Solution

Problem Statement

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 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.

Example 1:

bst tree

Input [“BSTIterator”, “next”, “next”, “hasNext”, “next”, “hasNext”, “next”, “hasNext”, “next”, “hasNext”] [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
Output [null, 3, 7, true, 9, true, 15, true, 20, false]
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);; // return 3; // return 7
bSTIterator.hasNext(); // return True; // return 9
bSTIterator.hasNext(); // return True; // return 15
bSTIterator.hasNext(); // return True; // return 20
bSTIterator.hasNext(); // return False

Binary Search Tree Iterator LeetCode Solution C++

class BSTIterator {
    stack<TreeNode *> myStack;
    BSTIterator(TreeNode *root) {

    bool hasNext() {
        return !myStack.empty();
    int next() {
        TreeNode *tmpNode =;
        return tmpNode->val;

    void pushAll(TreeNode *node) {
        for (; node != NULL; myStack.push(node), node = node->left);
};Code language: PHP (php)

Binary Search Tree Iterator LeetCode Solution Java

class BSTIterator {
    private Stack<TreeNode> stack = new Stack<TreeNode>();
    public BSTIterator(TreeNode root) {

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

    public int next() {
        TreeNode tmpNode = stack.pop();
        return tmpNode.val;
    private void pushAll(TreeNode node) {
        for (; node != null; stack.push(node), node = node.left);
}Code language: PHP (php)

Binary Search Tree Iterator Solution JavaScript

var BSTIterator = function(root) {
    if(!root){ = [];
    let visited = [];
    function traverse(node){
        if(node.left) traverse(node.left);
        if(node.right) traverse(node.right);
    traverse(root); = visited.reverse();
}; = function() {

BSTIterator.prototype.hasNext = function() {
    return > 0 ;
};Code language: JavaScript (javascript)

Binary Search Tree Iterator Solution Python

class BSTIterator(object):
    def __init__(self, root):
        self.stack = list()

    def hasNext(self):
        return self.stack

    def next(self):
        tmpNode = self.stack.pop()
        return tmpNode.val
    def pushAll(self, node):
        while node is not None:
            node = node.left
Scroll to Top