Advertisement

Path Sum II - LeetCode 113 Solution

Path Sum II - Complete Solution Guide

Path Sum II is LeetCode problem 113, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the root of a binary tree and an integer targetSum , return all root-to-leaf paths where the sum of the node values in the path equals targetSum . Each path should be returned as a list of the node values , not node references . A root-to-leaf path is a path starting from the root and ending at any leaf node. A leaf is a node with no children. Example 1: Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 Output: [[5,4,11,2],[5,8,4,5]] Explanation: There are two paths whos

Detailed Explanation

The problem asks us to find all paths from the root node to leaf nodes in a binary tree such that the sum of the node values along each path equals a given target sum. The output should be a list of lists, where each inner list represents a path with node values adding up to the target sum. A 'leaf' node is defined as a node with no children (neither left nor right child). We need to return the paths as lists of integers representing node *values*, not node references.

Solution Approach

The provided solutions use a recursive Depth-First Search (DFS) approach with backtracking. We start at the root node and recursively explore the left and right subtrees. At each node, we add its value to the current path and subtract it from the remaining sum. When we reach a leaf node, we check if the remaining sum is zero. If it is, we add the current path to the results. After exploring a subtree, we remove the current node's value from the current path (backtracking) so that we can explore other possible paths. This ensures we consider every possible root-to-leaf path.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty list `results` to store the paths that sum up to the `targetSum`.
  2. Step 2: Define a recursive function `dfs(node, current_path, remaining_sum)` to perform Depth-First Search.
  3. Step 3: Base Case: If the current node is `null`, return immediately because there's no path to explore.
  4. Step 4: Add the current node's value to the `current_path` and subtract it from the `remaining_sum`.
  5. Step 5: Check if the current node is a leaf node (no left and right children).
  6. Step 6: If it's a leaf node and `remaining_sum` is 0, then we found a valid path. Create a copy of the `current_path` and add it to the `results`.
  7. Step 7: If the current node is not a leaf node, recursively call `dfs` on the left and right children, passing the updated `current_path` and `remaining_sum`.
  8. Step 8: Backtrack: After exploring the left and right subtrees, remove the last element from the `current_path`. This step is crucial to explore different branches of the tree.
  9. Step 9: Call the `dfs` function with the `root`, an empty `current_path`, and the `targetSum`.
  10. Step 10: Return the `results` list containing all the paths that sum up to the `targetSum`.

Key Insights

  • Insight 1: The core idea is to use Depth-First Search (DFS) to explore all possible paths from the root to the leaves.
  • Insight 2: Backtracking is crucial to explore different branches of the tree. After exploring a path, we need to 'backtrack' (undo the choices) to explore other paths.
  • Insight 3: We need to keep track of the current path and the remaining sum. When we reach a leaf node, we check if the remaining sum is zero. If it is, we have found a valid path.

Complexity Analysis

Time Complexity: O(N)

Space Complexity: O(N)

Topics

This problem involves: Backtracking, Tree, Depth-First Search, Binary Tree.

Companies

Asked at: Arista Networks, DoorDash, Flipkart, Oracle, TikTok, Walmart Labs.