Maximum sum path from the leaf to root

Easy
0/40
Average time to solve is 10m
profile
Contributed by
17 upvotes
Asked in companies
OYOSprinklrSamsung

Problem statement

You are given a binary tree of 'N' nodes.

Your task is to find the path from the leaf node to the root node which has the maximum path sum among all the root to leaf paths.

For Example:

sample tree

All the possible root to leaf paths are:
3, 4, -2, 4 with sum 9
5, 3, 4 with sum 12
6, 3, 4 with sum 13
Here, the maximum sum is 13. Thus, the output path will be 6, 3, 4.

Note:

There will be only 1 path with max sum.
Detailed explanation ( Input/output format, Notes, Images )
Input format:
The very first line of input contains an integer 'T' denoting the number of queries or test cases. 

The first and only line of every test case contains elements of the binary tree in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take 0 in its place.

For example, the level order input for the tree depicted in the above image would be :

4
-2 3
4 0 5 6
0 3 0 0 0 0
0 0

Explanation :

Level 1 :
The root node of the tree is 4

Level 2 :
Left child of 4 = -2
Right child of 4 = 3

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

Level 4 :
Left child of 4 = null (0)
Right child of 4 = 3
Left child of 5 = null (0)
Right child of 5 = null (0)
Left child of 6 = null (0)
Right child of 6 = null (0)

Level 5 :
Left child of 3 = null (0)
Right child of 3 = null (0)
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:

4 -2 3 4 0 5 6 0 7 0 0 0 0 0 0
Output format:
For each test case, print integers representing the path from the leaf node to the root node which has the maximum sum separated by spaces in a single line. 
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function. 
Constraints:
1 <= T <= 100
1 <= N <= 3000
-10 ^ 5 <= DATA <= 10 ^ 5, DATA != 0

where 'T' is the number of test cases, 'N' is the number of nodes in the tree and 'DATA' is the value of each node.

Time limit: 1 sec
Sample input 1:
2
4 -2 3 4 0 5 6 0 3 0 0 0 0 0 0
1 0 2 0 0
Sample output 1:
6 3 4
2 1
Explanation for Sample Input 1:
For test case 1: Refer to the example given above in the problem description.

For test case2: The tree given in the input can be represented as:-

test case 2

There is only 1 possible path from the leaf to root which is 2, 1 with sum 3. So, this will be our answer.
Sample input 2:
2
4 3 2 -10 0 3 1 0 0 0 0 0 0
6 1 3 5 0 0 2 0 0 0 0
Sample output 2:
3 2 4
5 1 6
Hint

The idea is to use recursion to get the maximum sum path. We can recursively get the max sum path from the left and right subtree, then, we can choose the one with the maximum sum.

Approaches (2)
Recursive Subproblem

The idea here is that we will do a recursive solution by asking the children of the current node for the max sum path and then choose the path with the max sum. 

 

The approach will be as follows:

  1. Get the max sum path for the left subtree.
  2. Get the max sum path for the right subtree.
  3. Select the one with the max sum from both and insert current node's data into it.
  4. Return the updated path.
     

Algorithm:

 

list<int> ‘MAXPATHSUM’('ROOT'):

  1. If the root is NULL: return empty list.
  2. Initialize ‘LEFTPATH’<int> = ‘MAXPATHSUM’('ROOT'->left).
  3. Initialize ‘LEFTPATH’<int> = ‘MAXPATHSUM’('ROOT'->right).
  4. Append 'ROOT' -> data to both the lists.
  5. Find the sum of both lists.
  6. Return the list with the greater sum.
Time Complexity

O(N + H ^ 2), where ‘N’ is the number of nodes in the Binary tree and ‘H’ is the height of the binary tree. 

 

In the worst case, for each level, we will be storing the path from the leaf node to every level and calculating the sum for the same.

Note: If the given tree is a skew tree, then time complexity will be O(N ^ 2).

Space Complexity

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

 

In the worst case, we are storing at max 2 * N nodes.

Code Solution
(100% EXP penalty)
Maximum sum path from the leaf to root
Full screen
Console