一、leetcode地址
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
二、问题描述
三、代码实现
语言:Python3
代码:
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if p.val > q.val:
p,q = q,p
if root.val >= p.val and root.val <= q.val:
return root
elif root.val < p.val:
return self.lowestCommonAncestor(root.right, p, q)
else:
return self.lowestCommonAncestor(root.left, p, q)