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)
原文地址:https://www.cnblogs.com/luo-c/p/12857333.html