236.二叉树的最小公共祖先

class Solution(object):
    def __index__(self):
        self.ans = None
    def lowestCommonAncestor(self,root,p,q):
        def recurse_tree(c_node):
            if not c_node:
                return False
            left = recurse_tree(c_node.left)
            right = recurse_tree(c_node.right)
            mid = c_node==p or c_node==q
            if mid+left+right>=2:
                self.ans = c_node
            return mid or left or right
        recurse_tree(root)
        return self.ans

你可能感兴趣的:(236.二叉树的最小公共祖先)