Convert a BST into a Generator

Here's how you convert a Binary Search Tree into a (Python) Generator.

Note: we're using in-order traversal here.

def bstToGen(self, root):
    
    # Access root node's left (if it exists)
    if root.left:
        yield from self.bstToGen(root.left)

    # Access the root node
    yield root
    
    # Access root node's right (if it exists)
    if root.right:
        yield from self.bstToGen(root.right)
        

def solve(self, root):
    bstGenerator = self.bstToGen(root)

    head = next(bstGenerator)
    
    # Your remaining code...