 All Problems
Path Sum
easy
tree
depth-first search
breadth-first search
binary tree
amazon
microsoft

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:
5 4 8 11 null 13 4 7 2 null null null 1
22
Output: true
Explanation: 5 → 4 → 11 → 2 = 22

Example 2:

Input:
1 2 3
5
Output: false

Constraints:

  • The number of nodes in the tree is in the range [0, 5000]
  • -1000 ≤ Node.val ≤ 1000
  • -1000 ≤ targetSum ≤ 1000

Input format: First line: BFS level-order (null for missing). Second line: targetSum.

Output format: true or false.

Run to check your code against the sample cases, or submit to run every case