Given a binary tree of **N** edges, where every node contains distinct data. 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 number of testcases T. For each testcase, there will be two lines, first of which containing the number of edges (between two nodes) in the tree. Next line contains N pairs (considering **a** and **b**) with a '**L**' (means node b on left of a) or '**R**' (means node b on right of a) after a and b.

**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 <= N <= 10^{3}

**Example:
Input:**

2

6

10 2 L 2 20 L 2 1 R 10 -25 R -25 3 L -25 4 R

3

10 2 L 10 5 R 5 -2 R

**Output:**

32

17

**Explanation:
Testcase 2:** Path in the given tree goes like 2, 10, 5, which gives the max sum as 17.

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.

PM_Pranay | 38 |

m_raju | 22 |

Piyush_43 | 22 |

abducodes | 22 |

RamduttSharma | 18 |

Pulkit__Sharma__ | 626 |

Anirban166 | 503 |

abducodes | 468 |

Found_me | 430 |

Yo_Nehru | 404 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4559 |

Login to report an issue on this page.