Arena / Problems /

112. Path Sum

Easy 0/10 Thrones Claimed
Prev Next

Problem Specification

LeetCode #112

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.

Example 1
Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output: true
Explanation:

The root-to-leaf path with the target sum is shown.

Example 2
Input: root = [1,2,3], targetSum = 5
Output: false
Explanation:
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.
Example 3
Input: root = [], targetSum = 0
Output: false
Explanation:

Since the tree is empty, there are no root-to-leaf paths.

Constraints
  • The number of nodes in the tree is in the range [0, 5000].
  • -1000 <= Node.val <= 1000
  • -1000 <= targetSum <= 1000
⚔️

Vacant Python Throne

No 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.

Contenders & Community Submissions (2)

Peer solutions & challenges
#1 Depth First Search (with one `sum`) by Anonymous Developer
0
# 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
#2 Depth First Search (with `sum` as param) by Anonymous Developer
0
# 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
⚔️

Vacant Java Throne

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.

⚔️

Vacant C++ Throne

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.

⚔️

Vacant Javascript Throne

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.

⚔️

Vacant Go Throne

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.

⚔️

Vacant Rust Throne

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.

⚔️

Vacant C# Throne

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.

⚔️

Vacant Ruby Throne

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.

⚔️

Vacant Swift Throne

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.

⚔️

Vacant Kotlin Throne

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.