bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 112. Path Sum

112. Path Sum

Leetcode Solutions

Recursive Approach to Find Root-to-Leaf Path Sum

  1. Check if the current node is null. If it is, return false since we cannot find a path in an empty tree.
  2. Subtract the node's value from the target sum.
  3. Check if the current node is a leaf (no children) and if the updated target sum is 0. If both conditions are met, return true.
  4. Recursively call the function for the left and right children of the current node, passing the updated target sum.
  5. Return true if either recursive call returns true, indicating a valid path was found in either subtree.
  6. If no valid path is found, return false.
UML Thumbnail

Iterative Approach to Find Root-to-Leaf Path Sum

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...