递归与二叉树_leetcode112

class Solution(object):
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if not root:
return False

if root and not root.left and not root.right:
return sum == root.val
if self.hasPathSum(root.left,sum-root.val):
return True
if self.hasPathSum(root.right,sum-root.val):
return True
return False

posted @ 2019-03-17 14:00  AceKo  阅读(89)  评论(0编辑  收藏  举报