题目描述:
链接:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
代码:
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
a=root.val
b=p.val
c=q.val
if b>a and c>a:
return self.lowestCommonAncestor(root.right,p,q)
elif b<a and c<a:
return self.lowestCommonAncestor(root.left,p,q)
else:
return root