Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

120. Path Sum

EasyDay 35 of 60Trees & BST

Given the root of a binary tree and an integer targetSum, return true if some path from the root down to a leaf has node values adding up to 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
The path 5, 4, 11, 2 sums to 22.
Example 2
Input: root = [1,2,3], targetSum = 5
Output: false
Constraints
  • The number of nodes is in the range 0 to 5000
  • -1000 <= Node.val <= 1000
  • -1000 <= targetSum <= 1000
Hints
Frequently asked at: Amazon, Microsoft, Meta
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min