236-Lowest Common Ancestor of a Binary Tree

题目:普通二叉树的最小公共节点

def lowest_CommonAncestor(root,p,q):
    if root == None or root == p or root == q:
        return root
    left = lowest_CommonAncestor(root.left,p,q)
    right = lowest_CommonAncestor(root.right,p,q)

    if left == None:
        return right
    if right == None:
        return left

    return root

注:

如果p,q指向根节点,即该节点为最小公共节点。然后遍历左、右子树。如果遍历左子树为空,则返回右子树的结果,如果右子树为空,则返回左子树的结果,如果都不为空,则返回此时传入的根节点。

原文地址:https://www.cnblogs.com/kingshine007/p/11373099.html