236_二叉树的最近公共祖先



def lowestCommonAncestor(root, p, q):
    if not root or root == p or root == q:
        return root

    left = lowestCommonAncestor(root.left, p, q)
    right = lowestCommonAncestor(root.right, p, q)

    if left and right:
        return root
    if left:
        return left
    if right:
        return right

    return None



©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页