Given a binary tree, the task is to find the maximum path sum. The path may start and end at any node in the tree.
Input:
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 denotes 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
Output:
Print the maximum path sum in the binary tree.
User Task:
The task is to complete the findMaxSum() that takes root as input and returns max sum.
Constraints:
1 <= T <= 100
1 <= Number of nodes <= 10^{3}
1 <= |Data on node| <= 10^{4}
Example:
Input:
2
10 2 -25 20 1 3 4
10 2 5 N N N -2
Output:
32
17
Explanation:
Testcase 2: Path in the given tree goes like 2, 10, 5, which gives the max sum as 17.
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.
akhyasharma01 | 467 |
ASWATHAMA | 399 |
john_wick | 360 |
mmzeineddine | 295 |
PiyushPandey4 | 271 |
PiyushPandey4 | 614 |
ASWATHAMA | 561 |
akhyasharma01 | 547 |
john_wick | 521 |
rahulnishad96 | 442 |
blackshadows | 5362 |
Ibrahim Nash | 5242 |
akhayrutdinov | 5111 |
mb1973 | 4929 |
Quandray | 4598 |