Skip to content

Latest commit

 

History

History
63 lines (46 loc) · 1.56 KB

_173. Binary Search Tree Iterator.md

File metadata and controls

63 lines (46 loc) · 1.56 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 26, 2024

Last updated : June 26, 2024


Related Topics : Stack, Tree, Design, Binary Search Tree, Binary Tree, Iterator

Acceptance Rate : 73.3 %


Solutions

Python

# 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: Optional[TreeNode]):
        self.val = root.val
        self.stk = []

        while root :
            self.stk.append(root)
            root = root.left

    def next(self) -> int:
        curr = self.stk.pop()
        output = curr.val

        if curr.right :
            curr = curr.right
            while curr :
                self.stk.append(curr)
                curr = curr.left
        
        return output

    def hasNext(self) -> bool:
        return len(self.stk) > 0


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