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.

bhatabhi554 | 317 |

hardikJain5 | 306 |

CodeBuddy | 293 |

Akkki111 | 233 |

clone | 230 |

KshatriyaYash | 1932 |

nikhil_sojan | 1374 |

lonecoder | 1236 |

mazumderrohit8 | 1176 |

RemoveTLE | 1144 |

blackshadows | 5327 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4605 |

Quandray | 4444 |

Login to report an issue on this page.