Maximum sum of Non-adjacent nodes
Submissions: 496   Accuracy:


  Difficulty: Medium   Marks: 4

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.

1 <= T <= 100
1 <= N <= 1000

Your Task:
This is a functional problem. User need to complete getMaxSum() function.


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

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

** For More Input/Output Examples Use 'Expected Output' option **

Author: SIDG

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.

Need help with your code? Please use, generate link and share the link here.

to report an issue on this page.