Binary Search Tree Iterator LeetCode Programming Solutions 2022 | LeetCode Problem Solutions in C++, Java, & Python [💯Correct]

Binary Search Tree Iterator LeetCode Solution | LeetCode Problems For Beginners | LeetCode Problems & Solutions | Improve Problem Solving Skills | LeetCode Problems Java | LeetCode Solutions in C++

Hello Programmers/Coders, Today we are going to share solutions to the Programming problems of LeetCode Solutions in C++, Java, & Python. At Each Problem with Successful submission with all Test Cases Passed, you will get a score or marks and LeetCode Coins. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for the Binary Search Tree Iterator in C++, Java & Python-LeetCode problem. We are providing the correct and tested solutions to coding problems present on LeetCode. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

About LeetCode

LeetCode is one of the most well-known online judge platforms to help you enhance your skills, expand your knowledge and prepare for technical interviews. 

LeetCode is for software engineers who are looking to practice technical questions and advance their skills. Mastering the questions in each level on LeetCode is a good way to prepare for technical interviews and keep your skills sharp. They also have a repository of solutions with the reasoning behind each step.

LeetCode has over 1,900 questions for you to practice, covering many different programming concepts. Every coding problem has a classification of either EasyMedium, or Hard.

LeetCode problems focus on algorithms and data structures. Here is some topic you can find problems on LeetCode:

  • Mathematics/Basic Logical Based Questions
  • Arrays
  • Strings
  • Hash Table
  • Dynamic Programming
  • Stack & Queue
  • Trees & Graphs
  • Greedy Algorithms
  • Breadth-First Search
  • Depth-First Search
  • Sorting & Searching
  • BST (Binary Search Tree)
  • Database
  • Linked List
  • Recursion, etc.

Leetcode has a huge number of test cases and questions from interviews too like Google, Amazon, Microsoft, Facebook, Adobe, Oracle, Linkedin, Goldman Sachs, etc. LeetCode helps you in getting a job in Top MNCs. To crack FAANG Companies, LeetCode problems can help you in building your logic.

Link for the ProblemBinary Search Tree Iterator– LeetCode Problem

Binary Search Tree Iterator– LeetCode Problem


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
["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]

[null, 3, 7, true, 9, true, 15, true, 20, false]

Explanation 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


  • The number of nodes in the tree is in the range [1, 105].
  • 0 <= Node.val <= 106
  • At most 105 calls will be made to hasNext, and next.
Binary Search Tree Iterator– LeetCode Solutions
Binary Search Tree Iterator Solution in C++:
class BSTIterator {
  BSTIterator(TreeNode* root) {

  /** @return the next smallest number */
  int next() {
    return vals[i++];

  /** @return whether we have a next smallest number */
  bool hasNext() {
    return i < vals.size();

  int i = 0;
  vector<int> vals;

  void inorder(TreeNode* root) {
    if (!root)

Binary Search Tree Iterator Solution in Java:
class BSTIterator {
  public BSTIterator(TreeNode root) {

  /** @return the next smallest number */
  public int next() {
    return vals.get(i++);

  /** @return whether we have a next smallest number */
  public boolean hasNext() {
    return i < vals.size();

  private int i = 0;
  private List<Integer> vals = new ArrayList<>();

  private void inorder(TreeNode root) {
    if (root == null)

Binary Search Tree Iterator Solution in Python:
class BSTIterator:
  def __init__(self, root: Optional[TreeNode]):
    self.stack = []

  def next(self) -> int:
    root = self.stack.pop()
    return root.val

  def hasNext(self) -> bool:
    return self.stack

  def pushLeftsUntilNone(self, root: Optional[TreeNode]):
    while root:
      root = root.left
  • Time: Constructor: O(n)O(n), next(): O(1)O(1), hasNext(): O(1)O(1)
  • Space: O(n)O(n)

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock