K - Sum Path In A Binary Tree

Moderate
0/80
Average time to solve is 35m
18 upvotes
Asked in companies
AdobeJP MorganWalmart

Problem statement

You are given a binary tree in which each node contains an integer value and a number ‘K’. Your task is to print every path of the binary tree with the sum of nodes in the path as ‘K’.

Note:

1. The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).

2. Output the paths in the order in which they exist in the tree from left to right. Example: In the below example, path {1,3} is followed by {3,1} and so on.

Example:

For K = 4 and tree given below:

alt text

The possible paths are:
1 3
3 1
-1 4 1
4 
-1 5

The sum of values of nodes of each of the above-mentioned paths gives a sum of 4.
Detailed explanation ( Input/output format, Notes, Images )
Input format
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines represent the ‘T’ test cases.

The first line of input contains the elements of the tree in the level order form separated by a single space.

If any node does not have a left or right child, take -1 in its place. Refer to the example below.

The second line of each test case contains an integer ‘K’ denoting the sum.
Example:
Elements are in the level order form. The input consists of values of nodes separated by a single space in a single line. In case a node is null, we take -1 in its place.

For example, the input for the tree depicted in the below image would be :

alt text

 1
 2 3
 4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1

Explanation : Level 1 : The root node of the tree is 1

Level 2 :
Left child of 1 = 2
Right child of 1 = 3

Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6

Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)

Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)

The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.

The input ends when all nodes at the last level are null (-1).
Note :
The above format was just to provide clarity on how the input is formed for a given tree. 

The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:

1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output format:
The output of each test case prints all the paths of the binary tree with the sum of nodes in the path as ‘K' in the order in which they exist in the tree from left to right.

The output of each test case is printed in a separate line.
Note
You are not required to print anything; it has already been taken care of. Just implement the function and return the answer.
Constraints:
1 <= T <= 50
1 <= N <= 10^2
-10^9 <= K <= 10^9
-10^7 <= DATA <= 10^7

Where ‘N’ is the number of nodes in the tree, ‘K’ denotes the sum, and 'DATA' denotes data contained in the node of a binary tree.

Time Limit: 1 sec
Sample Input 1:
2
10 5 -3 3 2 -1 11 3 -2 -1 1 -1 -1 -1 -1 -1 -1 -1 -1
8
1 3 -2 2 1 4 5 -1 -1 1 -1 1 2 -1 6 -1 -1 -1 -1 -1 -1 -1 -1 
5
Sample Output 1:
5 3    5 2 1    -3 11
3 2    3 1 1    1 3 1   4 1   1 -2 4 2     5   
Explanation For Sample Input 1:
Test case 1:

alt text

Clearly, the output contains the values of the nodes which sum up to 8 in the order in which they exist in the tree from left to right are:

Path 1: 5 3
Starting from node 5, we traverse to its left child. The sum of nodes is equal to 5 + 3 = 8, which is equal to 'K'.

Path 2: 5 2 1
Starting from node 5, we traverse to its right child which is 2, and then further to its right child which is 1. The sum of nodes is equal to 5 + 2 + 1= 8, which is equal to k.

Path 3: -3 11
Starting from node -3, we traverse to its right child. The sum of nodes is equal to -3 + 11= 8, which is equal to 'K'.


Test case 2:
The sum of values of the nodes which sum to 5 are:
3 2
3 1 1 
1 3 1 
4 1
1 -1 4 1
5
1 -1 5
Sample Input 2:
2
1 3 4 1 2 -1 6 -1 -1 -1 -1 -1 -1 
5
1 -2 2 5 3 -1 1 -1 -1 3 -1 -1 -1 -1 -1
4
Sample Output 2:
1 3 1   3 2   1 4
1 -2 5  -2 3 3    1 2 1
Hint

 Traverse the tree and store the required paths simultaneously.

Approaches (1)
Using Backtracking

The idea is to generate all possible paths and check whether any path sum is equal to ‘K’ or not. 

 

  1. If the ‘ROOT’ is NULL, return.
  2. Else, add the current node value to a vector.
  3. Recur for the left subtree.
  4. Recur for the right subtree.
  5. Traverse the vector from the back. The last element is taken as the node at which our current path terminates. Keep adding the elements of the vector and if the sum becomes equal to ‘K’, print the path.
  6. To backtrack remove the last element from the vector.
Time Complexity

O(N^2), where ‘N’ is the total number of nodes in the binary tree.

 

In the worst case, ‘N’ nodes are traversed and for each node a path of size ‘N’ is copied to a vector.

Space Complexity

O(N), where ‘N’ is the total number of nodes in the binary tree.

 

In the worst case, all the nodes of the binary tree are stored in a vector.

Code Solution
(100% EXP penalty)
K - Sum Path In A Binary Tree
Full screen
Console