Fastest Python Solution


  • 0
    M
    class BSTIterator(object):
        def __init__(self, root):
            vals = []
            def get_all_vals(node):
                if not node:
                    return
                get_all_vals(node.left)
                vals.append(node.val)
                get_all_vals(node.right)
            
            get_all_vals(root)
            
            self.vals = vals[::-1]
            
    
        def hasNext(self):
            return bool(self.vals)
    
        def next(self):
            return self.vals.pop()
    
    

Log in to reply
 

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