236 Lowest Common Ancestor of a Binary Tree

题目: 236 Lowest Common Ancestor of a Binary Tree

这道题和 235 基本一样

class Solution:
    # @param {TreeNode} root
    # @param {TreeNode} p
    # @param {TreeNode} q
    # @return {TreeNode}
    def lowestCommonAncestor(self, root, p, q):
        if root == None:
            return root
        if root == p or root == q:
            return root
        left = self.lowestCommonAncestor(root.left, p, q)
        right = self.lowestCommonAncestor(root.right, p, q)
        if left != None and right != None:
            return root
        if left != None:
            return left
        return right

 

posted @ 2015-07-13 14:16  dapanshe  阅读(105)  评论(0编辑  收藏  举报