All Paths for a Sum (medium)

Problem Statement #

Given a binary tree and a number ‘S’, find all paths from root-to-leaf such that the sum of all the node values of each path equals ‘S’.

Created with Fabric.js 1.6.0-rc.1 Example 1: 1 7 9 4 5 2 7 S: 12Output: 2Explaination: There are the two paths with sum '12':1 -> 7 -> 4 and 1 -> 9 -> 2
Created with Fabric.js 1.6.0-rc.1 Example 2: 12 7 1 4 10 5 S: 23Output: 2Explaination: Here are the two paths with sum '23':12 -> 7 -> 4 and 12 -> 1 -> 10

Try it yourself #

Try solving this question here:

Output

0.553s

Tree paths with sum 23: []

Solution #

This problem follows the Binary Tree Path Sum pattern. We can follow the same DFS approach. There will be two differences:

  1. Every time we find a root-to-leaf path, we will store it in a list.
  2. We will traverse all paths and will not stop processing after finding the first path.

Code #

Here is what our algorithm will look like:

Output

0.474s

Tree paths with sum 23: [[12, 7, 4], [12, 1, 10]]

Time complexity #

The time complexity of the above algorithm is O(N2)O(N^2), where ‘N’ is the total number of nodes in the tree. This is due to the fact that we traverse each node once (which will take O(N)O(N)), and for every leaf node we might have to store its path which will take O(N)O(N).

We can calculate a tighter time complexity of O(NlogN)O(NlogN) from the space complexity discussion below.

Space complexity #

If we ignore the space required for the allPaths list, the space complexity of the above algorithm will be O(N)O(N) in the worst case. This space will be used to store the recursion stack. The worst case will happen when the given tree is a linked list (i.e., every node has only one child).

How can we estimate the space used for the allPaths array? Take the example of the following balanced tree:

Created with Fabric.js 1.6.0-rc.1 1 2 3 4 5 6 7

Here we have seven nodes (i.e., N = 7). Since, for binary trees, there exists only one path to reach any leaf node, we can easily say that total root-to-leaf paths in a binary tree can’t be more than the number of leaves. As we know that there can’t be more than N/2N/2 leaves in a binary tree, therefore the maximum number of elements in allPaths will be O(N/2)=O(N)O(N/2) = O(N). Now, each of these paths can have many nodes in them. For a balanced binary tree (like above), each leaf node will be at maximum depth. As we know that the depth (or height) of a balanced binary tree is O(logN)O(logN) we can say that, at the most, each path can have logNlogN nodes in it. This means that the total size of the allPaths list will be O(NlogN)O(N*logN). If the tree is not balanced, we will still have the same worst-case space complexity.

From the above discussion, we can conclude that the overall space complexity of our algorithm is O(NlogN)O(N*logN).

Also from the above discussion, since for each leaf node, in the worst case, we have to copy log(N)log(N) nodes to store its path, therefore the time complexity of our algorithm will also be O(NlogN)O(N*logN).


Similar Problems #

Problem 1: Given a binary tree, return all root-to-leaf paths.

Solution: We can follow a similar approach. We just need to remove the “check for the path sum”.

Problem 2: Given a binary tree, find the root-to-leaf path with the maximum sum.

Solution: We need to find the path with the maximum sum. As we traverse all paths, we can keep track of the path with the maximum sum.

Mark as Completed
←    Back
Binary Tree Path Sum (easy)
Next    →
Sum of Path Numbers (medium)