class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if root == None:
return None
# 根据情况3,root是p或q其中一个(p和q必然是不同节点)时,应返回root
if root == p or root == q:
return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
#情况1
#左子和右子树的公共祖先都非空, 很明显root就是左右子树的公共祖先
if left != None and right != None:
return root
#情况2
if left == None and right == None:
return None
#情况3
#剩下的情况为:有且只有一个为空
return left if left != None else right