class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if root == p or root == q:
return root
a,b = root.val,min(p.val,q.val)
c = max(p.val,q.val)
if a > b and a < c:
return root
elif a < b:
return self.lowestCommonAncestor(root.right,p,q)
else:
return self.lowestCommonAncestor(root.left,p,q)