# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if root is None:
return False
sum = sum - root.val
if sum == 0 and root.left is None and root.right is None:
return True
# check left
left = self.hasPathSum(root.left, sum)
# check right
right = self.hasPathSum(root.right, sum)
return (left or right)
Download Path Sum.pyLeetcode 112 Path Sum problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.
self.val = x
Feedback is the most important part of any website.
If you have any query, suggestion or feedback, Please feel free to contact us.