您的位置:首页 > 编程语言 > Python开发

leetcode 236. Lowest Common Ancestor of a Binary Tree

2017-08-26 11:40 323 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息