# Definition for a binary tree node# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = NoneclassBSTIterator(object):def__init__(self,root):""" :type root: TreeNode """self.stack=[]self.pushLeft(root)defhasNext(self):""" :rtype: bool """returnself.stackdefnext(self):""" :rtype: int """top=self.stack.pop()# 右子树的最大值是下一个最大值self.pushLeft(top.right)returntop.valdefpushLeft(self,node):whilenode:self.stack.append(node)node=node.left# Your BSTIterator will be called like this:# i, v = BSTIterator(root), []# while i.hasNext(): v.append(i.next())