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.

bruceewayne | 175 |

manvirag982 | 158 |

Core_Ka_Bachha | 137 |

yash_sharan | 136 |

rajupraaa1234 | 133 |

mr_kksparrow | 433 |

manvirag982 | 374 |

rajupraaa1234 | 268 |

Exception_404 | 254 |

PranathiBhuvanagiri | 242 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.