BST Iterator Problem Statement

You are tasked with creating a class named BSTIterator that acts as an iterator for the inorder traversal of a binary search tree. Implement the following functions:

  1. BSTIterator(Node root): A constructor that inputs the root of the binary search tree and initializes the iterator.
  2. next(): Returns the next smallest element from the inorder traversal of the tree.
  3. hasNext(): Returns true if there remains a next element in the inorder traversal; otherwise, returns false.

Assume the binary search tree contains 'N' nodes, and you must print its inorder traversal using the iterator.

Input:

The first line of the input includes an integer T representing the number of test cases.
Each test case consists of a single line with nodes in level order form, where -1 indicates a null node.
For example, the input corresponds to a tree:

tree diagram

4
2 6
1 3 5 7
-1 -1 -1 -1 -1 -1

Output:

Output for each test case should be a line containing the inorder traversal of the binary search tree.

Example:

Input:
4 2 6 1 3 5 7 -1 -1 -1 -1 -1 -1
Output:
1 2 3 4 5 6 7

Explanation:

Level 1:
The root node of the tree is 4

Level 2:
Left child of 4 = 2
Right child of 4 = 6

Level 3:
Left child of 2 = 1
Right child of 2 = 3
Left child of 6 = 5
Right child of 6 = 7

Level 4:
Each child node at this level is null (-1), and the input concludes with all null nodes.

The tree is traversed in inorder, hence the output is 1 2 3 4 5 6 7.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 104
  • 1 <= A[i] <= 109
Note:
The input sequence is provided in a single line separated by spaces. Implementation of the given functions is sufficient as output is handled externally.
AnswerBot
4mo

Implement a BSTIterator class for inorder traversal of a binary search tree.

  • Create a BSTIterator class with constructor, next, and hasNext functions.

  • Use a stack to simulate inorder traversal of the bi...read more

Help your peers!
Select
Add answer anonymously...

Amazon Software Developer interview questions & answers

A Software Developer was asked 1mo agoQ. Could you describe the process for designing a data structure that allows for al...read more
A Software Developer was asked 1mo agoQ. What is MySQL?
A Software Developer was asked 1mo agoQ. What is Java?

Popular interview questions of Software Developer

A Software Developer was asked 1mo agoQ1. Could you describe the process for designing a data structure that allows for al...read more
A Software Developer was asked 1mo agoQ2. What is MySQL?
A Software Developer was asked 1mo agoQ3. What is Java?

Top HR questions asked in Amazon Software Developer

A Software Developer was asked 1mo agoQ1. Tell me about a time you had to get to the root cause of a problem
A Software Developer was asked 5mo agoQ2. What are the short-term and long-term goals for the team or organization?
A Software Developer was asked 5mo agoQ3. Why do you want to work at Amazon?
Amazon Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits