给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if root is None:
return None
if root.val > p.val and root.val > q.val: ## p,q都在左子树
return self.lowestCommonAncestor(root.left, p, q)
elif root.val < p.val and root.val < q.val: ## p,q都在右子树
return self.lowestCommonAncestor(root.right, p, q)
else: # p,q各在一边,说明当前的根就是最近共同祖先
return root