-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm173.py
35 lines (28 loc) · 870 Bytes
/
m173.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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()