forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
31 lines (26 loc) · 800 Bytes
/
Solution.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
# 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):
def inorder(root):
if root:
inorder(root.left)
self.vals.append(root.val)
inorder(root.right)
self.cur = 0
self.vals = []
inorder(root)
def next(self) -> int:
res = self.vals[self.cur]
self.cur += 1
return res
def hasNext(self) -> bool:
return self.cur < len(self.vals)
# Your BSTIterator object will be instantiated and called as such:
# obj = BSTIterator(root)
# param_1 = obj.next()
# param_2 = obj.hasNext()