Maximum path sum from any node
Submissions: 2345   Accuracy:

39.01%

  Difficulty: Medium   Marks: 4

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: 

  1. 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.

  2. 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 <= 103
1 <= |Data on node| <= 104

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.

 

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Ayush Govil
Author: Ayush Govil 1


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.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.