Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.
A leaf is a node with no children.
The root-to-leaf path with the target sum is shown.
There are two root-to-leaf paths in the tree:
(1 --> 2): The sum is 3.
(1 --> 3): The sum is 4.
There is no root-to-leaf path with sum = 5.
Since the tree is empty, there are no root-to-leaf paths.
[0, 5000].-1000 <= Node.val <= 1000-1000 <= targetSum <= 1000No developer has claimed the Python throne for LeetCode #112 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
# 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 | None, targetSum: int) -> bool:
if root is None:
return False
self.target_sum = targetSum
self.result = False
self.sum_ = root.val
self.traverse(root)
return self.result
def traverse(self, node):
if self.result:
return
if is_leaf(node) and self.sum_ == self.target_sum:
self.result = True
return
if node.left:
self.sum_ += node.left.val
self.traverse(node.left)
self.sum_ -= node.left.val # Rollback
if node.right:
self.sum_ += node.right.val
self.traverse(node.right)
self.sum_ -= node.right.val # Rollback
def is_leaf(node):
return node and node.left is None and node.right is None
# 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 | None, targetSum: int) -> bool:
if root is None:
return False
self.target_sum = targetSum
self.result = False
self.traverse(root, root.val)
return self.result
def traverse(self, node, sum_):
if self.result:
return
if is_leaf(node) and sum_ == self.target_sum:
self.result = True
return
if node.left:
self.traverse(node.left, sum_ + node.left.val)
if node.right:
self.traverse(node.right, sum_ + node.right.val)
def is_leaf(node):
return node and node.left is None and node.right is None
No developer has claimed the Java throne for LeetCode #112 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the C++ throne for LeetCode #112 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Javascript throne for LeetCode #112 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Go throne for LeetCode #112 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Rust throne for LeetCode #112 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the C# throne for LeetCode #112 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Ruby throne for LeetCode #112 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #112 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #112 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.