码迷,mamicode.com
首页 > 其他好文 > 详细

236 Lowest Common Ancestor of a Binary Tree

时间:2015-07-13 15:54:18      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

题目: 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

 

236 Lowest Common Ancestor of a Binary Tree

标签:

原文地址:http://www.cnblogs.com/dapanshe/p/4642758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!