# 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 = rightclassBSTIterator:def__init__(self,root: Optional[TreeNode]):definorder(node): res = []ifnot node:return [] res.extend(inorder(node.left)) res.append(node.val) res.extend(inorder(node.right))return res self.res =deque(inorder(root))defnext(self) ->int:return self.res.popleft()defhasNext(self) ->bool:returnlen(self.res)# Your BSTIterator object will be instantiated and called as such:# obj = BSTIterator(root)# param_1 = obj.next()# param_2 = obj.hasNext()