2019-01-26 08:34:45 +08:00

9 lines
338 B
Python

# straight forward recursive solution
def lowestCommonAncestor(self, root, p, q):
if not root: return None
if root.val > p.val and root.val > q.val:
return self.lowestCommonAncestor(root.left, p, q)
elif root.val < p.val and root.val < q.val:
return self.lowestCommonAncestor(root.right, p, q)
return root