Skip to content

Latest commit

 

History

History
41 lines (31 loc) · 937 Bytes

173.-binary-search-tree-iterator.md

File metadata and controls

41 lines (31 loc) · 937 Bytes

173. Binary Search Tree Iterator

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class BSTIterator:

    def __init__(self, root: TreeNode):
        self.stack = []
        while root:
            self.stack.append(root)
            root = root.left
        self.root = root

    def next(self) -> int:
        
        while self.root:
            self.stack.append(self.root)
            self.root = self.root.left
            
        node = self.stack.pop()
        self.root = node.right

        return node.val

    def hasNext(self) -> bool:
        if self.stack or self.root:
            return True
        return False


# Your BSTIterator object will be instantiated and called as such:
# obj = BSTIterator(root)
# param_1 = obj.next()
# param_2 = obj.hasNext()