112.Path Sum

 

 

 

 

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def hasPathSum(self, root: TreeNode, sum: int) -> bool:
        if root == None:
            return False
        if root.val == sum and root.left == None and root.right == None:
            return True
        if root.left == None and root.right != None:
            return self.hasPathSum(root.right, sum - root.val)
        if root.left != None and root.right == None:
            return self.hasPathSum(root.left, sum - root.val)
        return self.hasPathSum(root.left, sum-root.val) or self.hasPathSum(root.right, sum-root.val)

 

posted @ 2020-05-09 14:37  星海寻梦233  阅读(100)  评论(0编辑  收藏  举报