# Binary Search Tree Iterator

Medium

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.

Constraints:

• 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`.
``````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]

Explanation
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
bSTIterator.next();    // return 3
bSTIterator.next();    // return 7
bSTIterator.hasNext(); // return True
bSTIterator.next();    // return 9
bSTIterator.hasNext(); // return True
bSTIterator.next();    // return 15
bSTIterator.hasNext(); // return True
bSTIterator.next();    // return 20
bSTIterator.hasNext(); // return False``````

Solution:

```/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class BSTIterator {

Stack<TreeNode> stack;

public BSTIterator(TreeNode root) {

// Stack for the recursion simulation
this.stack = new Stack<TreeNode>();

// Remember that the algorithm starts with a call to the helper function
// with the root node as the input
this.leftMostInorder(root);
}

private void leftMostInorder(TreeNode root) {

// For a given node, add all the elements in the leftmost branch of the tree
// under it to the stack.
while (root != null) {
this.stack.push(root);
root = root.left;
}
}

/**
* @return the next smallest number
*/
public int next() {
// Node at the top of the stack is the next smallest element
TreeNode topNode = this.stack.pop();

// Need to maintain the invariant. If the node has a right child, call the
// helper function for the right child
if (topNode.right != null) {
this.leftMostInorder(topNode.right);
}

}

/**
* @return whether we have a next smallest number
*/
public boolean hasNext() {
return this.stack.size() > 0;
}
}
```

Complexity Analysis

Time complexity : The time complexity for this approach is very interesting to analyze. Let’s look at the complexities for both the functions in the class:

`hasNext()` is the easier of the lot since all we do in this is to return true if there are any elements left in the stack. Otherwise, we return false. So clearly, this is an `O(1)` operation every time. Let’s look at the more complicated function now to see if we satisfy all the requirements in the problem statement.

`next()` involves two major operations. One is where we pop an element from the stack which becomes the next smallest element to return. This is a `O(1)` operation. However, we then make a call to our helper function `leftMostInorder` which iterates over a bunch of nodes. This is clearly a linear time operation i.e. `O(N)` in the worst case. This is true.However, the important thing to note here is that we only make such a call for nodes which have a right child. Otherwise, we simply return. Also, even if we end up calling the helper function, it won’t always process `N` nodes. They will be much lesser. Only if we have a skewed tree would there be `N` nodes for the root. But that is the only node for which we would call the helper function.Thus, the amortized (average) time complexity for this function would still be `O(1)` which is what the question asks for. We don’t need to have a solution which gives constant time operations for every call. We need that complexity on average and that is what we get.

Space Complexity: The space complexity is `O(N)` (`N` is the number of nodes in the tree), which is occupied by our custom stack for simulating the inorder traversal. Again, we satisfy the space requirements as well as specified in the problem statement.

Categories: Binary Tree

Tagged as: , , ,