Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Path Sum

Easy
treebinary-treedfs

Given a binary tree and an integer targetSum, return true if some root-to-leaf path sums to targetSum.

Input

  • Line 1: the tree in level order
  • Line 2: integer targetSum

Output: true or false.

Example 1
Input:
5 4 8 11 null 13 4 7 2 null null null 1
22
Output:
true

5+4+11+2 = 22.

Example 2
Input:
1 2 3
5
Output:
false
Example 3
Input:
null
0
Output:
false

Constraints

  • 0 ≤ nodes ≤ 5000
  • -1000 ≤ values, targetSum ≤ 1000
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.