This is basically a iterative inorder traversal of binary tree:

```
class BSTIterator(object):
def __init__(self, root):
"""
:type root: TreeNode
"""
self.cur = root
self.stack =[]
def hasNext(self):
"""
:rtype: bool
"""
if self.stack or self.cur:
return True
return False
def next(self):
"""
:rtype: int
"""
while self.cur:
self.stack.append(self.cur)
self.cur = self.cur.left
self.cur = self.stack.pop()
res = self.cur.val
self.cur = self.cur.right
return res
```