DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
easy
Tree

Path Sum

**Problem Statement:** Given the required input arguments, write an efficient algorithm to solve the **Path Sum** problem. Implement the required logic as specified by standard definitions for this classic algorithmic challenge. **Hint / Expected Approach:** DFS subtract from target, check leaf **Edge Cases to Consider:** - (1) Negative target - (2) Root is null - (3) Internal node equals target but not leaf

Examples

Example 1
Input: [5,4,8,11,null,13,4,7,2,null,null,null,1], 22
Output: true
Explanation: Path 5→4→11→2=22
Example 2
Input: [1,2,3], 5
Output: false
Explanation: No path sums to 5
Example 3
Input: [], 0
Output: false
Explanation: Empty tree

Constraints

  • ▪The number of nodes in the tree is in the range [0, 2000]
  • ▪-1000 <= Node.val <= 1000
  • ▪Ensure depth-first or breadth-first traversals are memory-efficient

Watch Out For Edge Cases

  • ▪Negative target
  • ▪Root is null
  • ▪Internal node equals target but not leaf
Frequently Asked At
AmazonMicrosoftAdobeTCSInfosys