Path Sum

Easy

Company Tags

You are 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 = [1,2,3], targetSum = 3

Output: true

Explanation: The root-to-leaf path with the target sum is 1 -> 2.

Example 2:

Input: root = [-15,10,20,null,null,15,5,-5], targetSum = 15

Output: true

Explanation: The root-to-leaf path with the target sum is -15 -> 20 -> 15 -> -5.

Example 3:

Input: root = [1,1,0,1], targetSum = 2

Output: false

Constraints:

  • 0 <= The number of nodes in the tree <= 5000.
  • -1000 <= Node.val <= 1000
  • -1000 <= targetSum <= 1000


Company Tags

Please upgrade to NeetCode Pro to view company tags.

root =

targetSum =