Simple python solution


  • 0
    S
    class BSTIterator(object):
        def __init__(self, root):
            self.stack = []
            if root:
                self.populate(root)
    
        def hasNext(self):
            return self.stack
    
        def next(self):
            element = self.stack.pop()
            if element.right:
                self.populate(element.right)
            return element.val
            
        def populate(self, root):
            self.stack.append(root)
            while root.left:
                self.stack.append(root.left)
                root = root.left
    

Log in to reply
 

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