# Definition for a binary tree node # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class BSTIterator(object): def __init__(self, root): self.stack=[ ] while root: self.stack.append(root) root=root.left def hasNext(self): """ :rtype: bool """ return self.stack!=[ ] def next(self): """ :rtype: int """ p=self.stack.pop( ) res=p.val p=p.right while p: self.stack.append(p) p=p.left return res # Your BSTIterator will be called like this: # i, v = BSTIterator(root), [] # while i.hasNext(): v.append(i.next())