Lowest Common Ancestor in A Binary Search Tree

For a Binary Search Tree, the lowest common ancestor is the lowest node T that has both p and q as descendants (a node can be a descendant of itself)

There are three cases:

def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
    if p.val > root.val and q.val > root.val:
        return self.lowestCommonAncestor(root.right, p, q)
    elif p.val < root.val and q.val < root.val:
        return self.lowestCommonAncestor(root.left, p, q)
    else:
        return root