leetcode 236. Lowest Common Ancestor of a Binary Tree

来源:互联网 发布:淘客cms系统 编辑:程序博客网 时间:2024/05/16 18:03
class Solution(object):    def lowestCommonAncestor(self, root, p, q):        """        :type root: TreeNode        :type p: TreeNode        :type q: TreeNode        :rtype: TreeNode        """        if root in (p,q,None):            return root        left_res = self.lowestCommonAncestor(root.left,p,q)        right_res = self.lowestCommonAncestor(root.right,p,q)        if left_res and right_res:            return root        else:            return left_res if left_res else right_res

原创粉丝点击