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.
In above binary tree chosen nodes are encircled and are not directly connected and their sum is maximum possible.
We can solve this problem by considering the fact that both node and its children can’t be in sum at same time, so when we take a node into our sum we will call recursively for its grandchildren or when we don’t take this node we will call for all its children nodes and finally we will choose maximum from both of these results.
It can be seen easily that above approach can lead to solving same subproblem many times, for example in above diagram node 1 calls node 4 and 5 when its value is chosen and node 3 also calls them when its value is not chosen so these nodes are processed more than once. We can stop solving these nodes more than once by memoizing the result at all nodes.
In below code a map is used for memoizing the result which stores result of complete subtree rooted at a node in the map, so that if it is called again, the value is not calculated again instead stored value from map is returned directly.
Please see below code for better understanding.
Method 2 (Using pair)
Return a pair for each node in the binary tree such that first of the pair indicates maximum sum when the data of node is included and second indicates maximum sum when the data of a particular node is not included.
Time complexity: O(n)
Thanks to Surbhi Rastogi for suggesting this method.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming
- Sum of nodes at maximum depth of a Binary Tree
- Find maximum among all right nodes in Binary Tree
- Sum of nodes at maximum depth of a Binary Tree | Set 2
- Maximum sum of leaf nodes among all levels of the given binary tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Maximum length cycle that can be formed by joining two nodes of a binary tree
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Total sum except adjacent of a given node in a Binary Tree
- Maximum sum from a tree with adjacent levels not allowed
- Sum of all nodes in a binary tree
- Sink Odd nodes in Binary Tree
- Sum of nodes in top view of binary tree
- Sink even nodes in Binary Tree
- XOR of path between any two nodes in a Binary Tree
- Product of all nodes in a Binary Tree