```
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
if root.val == p.val or root.val == q.val:
return root
if p.val > q.val:
higher = p.val
lower = q.val
else:
higher = q.val
lower = p.val
if root.val > lower and root.val < higher:
return root
else:
if root.val > higher:
return self.lowestCommonAncestor(root.left, p, q)
else:
return self.lowestCommonAncestor(root.right, p, q)
```