Given a binary tree with a value associated with each node, we need to choose a subset of these nodes such that sum of chosen nodes is maximum under a constraint that no two chosen node in subset should be directly connected that is, if we have taken a node in our sum then we can’t take its any children in consideration and vice versa.

**Input Format: **

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer N denoting the number of edges. The next line contains edges of the binary tree.

**Output Format:**

Print the maximum sum such that no two nodes are adjacent.

**Constraints:**

1 <= T <= 100

1 <= N <= 1000

**Your Task:**

This is a functional problem. User need to complete **getMaxSum() **function.

**Example:
Input:**

1

5

1 2 L 1 3 R 2 4 L 3 5 L 3 6 R

16

**Explanation:**

**Testcase 1:** The maximum sum is sum of nodes **1 4 5 6 **, i.e **16**. These nodes are non adjacent.

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.

thanuvinu94 | 238 |

the_coder95 | 215 |

ShivayLamba | 208 |

sandeep.prajapati | 129 |

adityasuman2025 | 111 |

the_coder95 | 1441 |

RishabhTanwar1 | 1110 |

thanuvinu94 | 914 |

tathagat289 | 668 |

themanhasnoname | 620 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4897 |

Quandray | 4547 |

Login to report an issue on this page.