Given a binary tree in which each node element contains a number. Find the maximum possible sum from one leaf node to another.
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 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.
For each testcase, in a new line, print the maximum possible sum between two leaf nodes.
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.
1 <=T <= 100
3 <= N <= 104
-1000 <= Data on Node <= 1000
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
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.