BST Iterator

Moderate
0/80
Average time to solve is 20m
37 upvotes
Asked in companies
HSBCVisaFacebook

Problem statement

You are given a class named as BSTIterator that represents an iterator over inorder traversal of a binary search tree. You need to implement the following things as follows:

1. BSTIterator(Node root) - It is a parameterized constructor in which you are given the root of the Binary search tree. It will be called whenever an object of BSTIterator is created.

2. next() - This member function will return the next smallest element in the in-order traversal of the binary search tree. You need to implement this function.

3. hasNext() - This function will return true if there exists the next smallest element in the traversal else it will return false. You need to implement this function

The binary search tree has ‘N’ nodes you need to print the inorder traversal of the tree using the iterator.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains an integer ‘T’ denoting the number of test cases.

The first and the only line of each test case contains elements of the tree in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.

For example, the input for the tree depicted in the below image would be :

image

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

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 :
Left child of 1 = null (-1)
Right child of 1 = null (-1)
Left child of 3 = null (-1)
Right child of 3 = null (-1)
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 7 = null (-1)
Right child of 7 = null (-1)

The first not-null node(of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.

The input ends when all nodes at the last level are null(-1).
Note :
The above format was just to provide clarity on how the input is formed for a given tree.

The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:

4 2 6 1 3 5 7 -1 -1 -1 -1 -1 -1
Output Format:
The single line of output for each test case should contain the inorder traversal of the binary search tree.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10^4  
1 <= A[i] <= 10^9 

Where ‘T’ is the number of test cases, ‘N’ is the number of nodes, and A[i] is the value of a node.

Time Limit: 1 sec
Sample Input 1:
2
2 1 3 -1 -1 -1 -1
10 5 -1 2 -1 -1 -1
Sample Output 1:
1 2 3
2 5 10
Explanation for sample input 1:
Test case 1:
The tree will look like this :

image

The inorder traversal of the tree will be 1, 2, 3.

Test case 2:
The tree will look like this:

image

The inorder traversal of the tree will be 2, 5, 10.
Sample Input 2:
2
6 -1 7 -1 8 -1 -1    
3 2 4 1 -1 -1 -1 -1 -1
Sample Output 2:
6 7 8
1 2 3 4
Hint

Do some preprocessing to store nodes in sorted order

Approaches (2)
Tree flattening
  • We need to implement two functions next() and hasNext(). The next() function will move the iterator to the right and return the next smaller element and the hasNext() function will return true if there exists the next smallest element else false.
  • We can prepare a list that stores all the elements of the binary search tree in sorted order. We can do so by performing inorder traversal on the given binary search tree and store the elements in the list. As the inorder traversal gives us a sorted order of elements so the list will contain the elements in sorted order.
  • We maintain a variable to iterate over the list that we prepared in the previous step. Now for each call of the next() function, we simply move on the list one step towards the right and return the value stored therein the array. As the array is sorted so it will be the next smaller element.
  • For the call of the hasNext() function, we need to check if our iterator reached the end of the array or not. If it is reached to the end of the array that means no element is left we iterate over all the elements so we return false else we will return true.
Time Complexity

O(N), where ‘N’ is the number of nodes in the binary search tree.



We are traversing each node one time while performing inorder traversal that will take O(N) time complexity.

Each call of the next() function will work in O(1) complexity.

Each call of the hasNext() function will work in O(1) complexity.

Thus, the final time complexity is O(N).

Space Complexity

O(N), where ‘N’ is the number of nodes in the binary search tree.



We are using a list of size ‘N’ to store elements of the binary search tree.
 

Code Solution
(100% EXP penalty)
BST Iterator
Full screen
Console