Concise Python solution without auxiliary functions

  • 0

    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.cur = self.cur.left
            self.cur = self.stack.pop()
            res = self.cur.val
            self.cur = self.cur.right
            return res

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.