Binary Tree Path Sum (easy)

Problem Statement #

Given a binary tree and a number ā€˜S’, find if the tree has a path from root-to-leaf such that the sum of all the node values of that path equals ā€˜S’.

Created with Fabric.js 1.6.0-rc.1 1 2 3 4 5 6 7 S: 10Output: trueExplaination: The path with sum '10' is highlighted Example 1:
Created with Fabric.js 1.6.0-rc.1 Example 2: 12 7 1 9 10 5 S: 23Output: trueExplaination: The path with sum '23' is highlighted S: 16Output: falseExplaination: There is no root-to-leaf path with sum '16'.

Try it yourself #

Try solving this question here:

Output

0.517s

Tree has path: False Tree has path: False

Solution #

As we are trying to search for a root-to-leaf path, we can use the Depth First Search (DFS) technique to solve this problem.

To recursively traverse a binary tree in a DFS fashion, we can start from the root and at every step, make two recursive calls one for the left and one for the right child.

Here are the steps for our Binary Tree Path Sum problem:

  1. Start DFS with the root of the tree.
  2. If the current node is not a leaf node, do two things:
    • Subtract the value of the current node from the given number to get a new sum => S = S - node.value
    • Make two recursive calls for both the children of the current node with the new number calculated in the previous step.
  3. At every step, see if the current node being visited is a leaf node and if its value is equal to the given number ā€˜S’. If both these conditions are true, we have found the required root-to-leaf path, therefore return true.
  4. If the current node is a leaf but its value is not equal to the given number ā€˜S’, return false.

Let’s take the example-2 mentioned above to visually see our algorithm:

Created with Fabric.js 1.6.0-rc.1 12 7 1 9 10 5 S: 23
Let's start with the root node
1 of 10
Created with Fabric.js 1.6.0-rc.1 12 7 1 9 10 5 S: 23
Not a leaf node, make recursive calls for children.
2 of 10
Created with Fabric.js 1.6.0-rc.1 12 7 1 9 10 5 S: 11
Not a leaf node, make recursive call for the left child.
3 of 10
Created with Fabric.js 1.6.0-rc.1 12 7 1 9 10 5 S: 4
Leaf node, but S != 9, therefore return false.
4 of 10
Created with Fabric.js 1.6.0-rc.1 12 7 1 9 10 5 S: 11
After traversing the left-child, make a recursive call for the right child. This recursive all will return false, as the right child is 'null'.
5 of 10
Created with Fabric.js 1.6.0-rc.1 12 7 1 9 10 5 S: 23
After traversing the left-child, make a recursive call for the right child, as the left-child failed in finding the path.
6 of 10
Created with Fabric.js 1.6.0-rc.1 12 7 1 9 10 5 S: 11
Not a leaf node, make recursive call for the left child.
7 of 10
Created with Fabric.js 1.6.0-rc.1 12 7 1 9 10 5 S: 10
Leaf node, but S == 10, we have found a path; therefore return true.
8 of 10
Created with Fabric.js 1.6.0-rc.1 12 7 1 9 10 5 S: 11
As the left-child returned 'true', return 'true' without processing further.
9 of 10
Created with Fabric.js 1.6.0-rc.1 12 7 1 9 10 5 S: 23
As the right-child returned 'true', return 'true', we have found the path.
10 of 10

Code #

Here is what our algorithm will look like:

Output

0.622s

Tree has path: True Tree has path: False

Time complexity #

The time complexity of the above algorithm is O(N)O(N), where ā€˜N’ is the total number of nodes in the tree. This is due to the fact that we traverse each node once.

Space complexity #

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).

Mark as Completed
←    Back
Introduction
Next    →
All Paths for a Sum (medium)