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.

&nbsp;
Example 1:

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


&nbsp;
Constraints:


	The number of nodes in the tree is in the range [1, 105].
	0 &lt;= Node.val &lt;= 106
	At most 105 calls will be made to hasNext, and next.


&nbsp;
Follow up:


	Could you implement next() and hasNext() to run in average O(1) time and use&nbsp;O(h) memory, where h is the height of the tree?


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class BSTIterator:

    def __init__(self, root: Optional[TreeNode]):
        self.root=root
        self.tree=[]#list to store the inorder traversal
        def inorder(node):
            if not node:
                return
            inorder(node.left)
            self.tree.append(node.val)
            inorder(node.right)
            return
        inorder(self.root)
        self.i=0
        

    def next(self) -> int:
        self.i+=1
        return self.tree[self.i-1]

    def hasNext(self) -> bool:
        return self.i-1<len(self.tree)-1


# Your BSTIterator object will be instantiated and called as such:
# obj = BSTIterator(root)
# param_1 = obj.next()
# param_2 = obj.hasNext()
class BSTIterator {

    TreeNode root;
    TreeNode current;
    Stack<TreeNode> st = new Stack<>();
    public BSTIterator(TreeNode root) {
        this.root = root;
        //init(root);
        current = findLeft(root);
        //System.out.println("Init: stack is: "+st);
    }

    public int next() {
        
        int val = -1;
        if(current != null)
            val = current.val;
        else
            return -1;
        
        if(current.right != null)
            current = findLeft(current.right);
        else if(!st.isEmpty())
            current = st.pop();
         else   
            current = null;
       // System.out.println("next: stack is: "+st);
        return val;
    }
    
    public TreeNode findLeft(TreeNode node) {
        
        if(node == null)
            return null;
        
        if(node.left != null){
            TreeNode next = node.left;
            st.push(node);
            return findLeft(next);
        }
        else
            return node;
        
    }
    
    public boolean hasNext() {
        return current != null;
    }
}
//TC O(1) and O(H) Space

//MIMIC inorder
class BSTIterator {
public:
    stack<TreeNode*> stk;
    BSTIterator(TreeNode* root) {
        pushAll(root);
         // left is done
    }
    
    int next() {
        //root handled
        TreeNode* node = stk.top();
        int ans = node->val;
        stk.pop();
        
        //right handled
        pushAll(node->right);
        
        return ans;
        
    }
    
    bool hasNext() {
        return stk.size() != 0; // stk is empty then no next to show simple
    }
    
    void pushAll(TreeNode* root){
        //left part - as inorder is like Left left left, once a root is done then check right
        while(root!= NULL){
            stk.push(root);
            root = root->left;
        }
        
    }
};
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 */
var BSTIterator = function(root) {
    this.stack = [];
    this.node = root;
};

/**
 * @return {number}
 */
BSTIterator.prototype.next = function() {
    while (this.node) {
        this.stack.push(this.node);
        this.node = this.node.left;
    }
    const node = this.stack.pop();
    this.node = node.right;
    return node.val;
};

/**
 * @return {boolean}
 */
BSTIterator.prototype.hasNext = function() {
    return this.node || this.stack.length > 0;
};

/** 
 * Your BSTIterator object will be instantiated and called as such:
 * var obj = new BSTIterator(root)
 * var param_1 = obj.next()
 * var param_2 = obj.hasNext()
 */
Binary Search Tree Iterator