Path Sum
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.

30:00

Path Sum
easy
Topics
Companies

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.

Example 1:
Input: {"root":[5,4,8,11,null,13,4,7,2,null,null,null,1],"targetSum":22}
Output: true
Constraints:
  • The number of nodes is in the range [0,5000][0, 5000].

  • 1000Node.val1000-1000 \leq \text{Node.val} \leq 1000

  • 1000targetSum1000-1000 \leq \text{targetSum} \leq 1000

Input
arr ={"root":[5,4,8,11,null,13,4,7,2,null,null,null,1],"targetSum":22}
Target
22

5

4

8

11

13

4

7

2

1

Variables
VariableValue
val5
remSum22
DepthFunction Call
0hasPathSumnode=5, remSum=22
0/12