```
class Solution(object):
def lowestCommonAncestor(self, root, p, q):
v1, v2 = min(p.val, q.val), max(p.val, q.val)
if v1 <= root.val <= v2:
return root
if v1 > root.val:
return self.lowestCommonAncestor(root.right, p, q)
return self.lowestCommonAncestor(root.left, p, q)
```