text: # Definition for singly-linked list.
  # class ListNode:
  #     def __init__(self, val=0, next=None):
  #         self.val = val
  #         self.next = next
  # 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 Solution:
      def sortedListToBST(self, head: ListNode) -> TreeNode:
          def getMedian(left: ListNode, right: ListNode) -> ListNode:
              fast = slow = left
              while fast != right and fast.next != right:
                  fast = fast.next.next
                  slow = slow.next
              return slow
  
          def recursive(left, right):
              if left == right: return None
              mid = getMedian(left, right)
              root = TreeNode(mid.val)
              root.left = recursive(left, mid)
              root.right = recursive(mid.next, right)
              return root 
  
          return recursive(head, None)
  