9 lines
338 B
Python
Raw Normal View History

2018-12-22 15:50:46 +11:00
# 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