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:**

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

mr_kksparrow | 433 |

manvirag982 | 272 |

snow_den_ | 236 |

arpit_anshuman | 228 |

Exception_404 | 220 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.