173. Binary Search Tree Iterator

class BSTIterator(object):
    def __init__(self, root):
        """
        :type root: TreeNode
        """
        self.stack = []
        self.pushall(root)

    def hasNext(self):
        """
        :rtype: bool
        """
        return self.stack

    def next(self):
        """
        :rtype: int
        """
        cur_node = self.stack.pop()
        self.pushall(cur_node.right)
        return cur_node.val

    def pushall(self, root):
        while root is not None:
            self.stack.append(root)
            root = root.left

results matching ""

    No results matching ""