Associated Course(s):
Must Do Interview Preparation

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 input contains **T**, denoting number of testcases. For each testcase there will be two lines. The first line contains number of edges. The second line contains two nodes and a character separated by space. The first node denotes data value, second node denotes where it will be assigned to the previous node which will depend on character '**L**' or '**R**' i.e. the 2nd node will be assigned as left child to the 1st node if character is 'L' and so on. The first node of second line is **root node**. The struct or class **Node** has a data part which stores the data, pointer to left child and pointer to right child. 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 he maximum possible sum.

**Your Task:**

This is a function problem. You only need to complete the **function maxPathSum **that returns the maximum sum from one lead to other.

**Constraints:**

1 <=T <= 100

1 <= N <= 30

**Example:
Input:**

1

12

-15 5 L -15 6 R 5 -8 L 5 1 R -8 2 L -8 -3 R 6 3 L 6 9 R 9 0 R 0 4 L 0 -1 R -1 10 L

**Output:**

27

**Explanation:
Testcase 1:** The maximum possible sum from one leaf node to another is (3 + 6 + 9 + 0 + -1 + 10 = 27)

Author: saksham seth

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.

d_Coder00 | 163 |

VivSisMMM | 152 |

Astikeysingh | 148 |

Reetu_Kumari_MIT2018056 | 132 |

dungeon_master1299 | 126 |

Pulkit__Sharma__ | 658 |

Anirban166 | 531 |

abducodes | 518 |

Found_me | 494 |

Sathvik Avasarala | 466 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4559 |

Login to report an issue on this page.