Given a binary tree in which each node element contains a number. Find the maximum possible sum from one leaf node to another.
Input Format:
The first line of input contains the number of test cases T. For each test case, there will be only a single line of input which is a string representing the tree as described below:
The values in the string are in the order of level order traversal of the tree where, numbers denote node values, and a character “N” denotes NULL child.
For example:
For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N
There are multiple test cases. For each test case, the function will be called individually.
Output Format:
For each testcase, in a new line, print the maximum possible sum between two leaf nodes.
Your Task:
Complete the function maxPathSum() that returns the maximum sum from one leaf to another.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(H).
Note: H is the height of the tree and this space is used implicitly for recursion stack.
Constraints:
1 <=T <= 100
3 <= N <= 10^{4}
-1000 <= Data on Node <= 1000
Example:
Input:
2
3 4 5 -10 4
-15 5 6 -8 1 3 9 2 -3 N N N N N 0 N N N N 4 -1 N N 10 N
Output:
16
27
Explanation:
Testcase 2: The maximum possible sum from one leaf node to another is (3 + 6 + 9 + 0 + -1 + 10 = 27)
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.
tekuripurushotham | 64 |
omkarjadhav1191 | 63 |
visutkarsh2000 | 62 |
ayushaggarwal821 | 51 |
dixitabhyuday | 38 |
abnandi04 | 776 |
ponnadasaikesav | 450 |
KshamaGupta | 446 |
yogesh_aka_dante | 405 |
mummy | 313 |
blackshadows | 5362 |
Ibrahim Nash | 5244 |
akhayrutdinov | 5111 |
mb1973 | 4976 |
Quandray | 4752 |