Given a N-ary tree with a value associated with each node, the task is 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.
The above diagram selects the nodes with a deep green color to get the maximum value 25.
An approach to this problem has been discussed in the previous post using recursion.
In this post, we will be discussing an approach using Dynamic Programming on Trees.
While solving the problem, there arise two cases:
- For a particular node, the maximum sum can be calculated by including the node itself along with nodes from its subtree.
- Or, the maximum sum is calculated by excluding the current node and including only the nodes from its subtree.
Let us assume:
- dp1[node] to be the maximum possible sum by choosing nodes from the subtree of this node and also including the node.
- And, dp2[node] to be the maximum possible sum by choosing nodes from the subtree of the node and not including the node itself.
In the first case, if we include the current node, then its value is added and then we can not include any of its immediate children, hence the summation of dp2 of all the children will be taken into the count to compute dp1[node]. That is,
dp1[node] = tree[node] + sum(dp2[children1], dp2[children2], …)
In the second case, if we do not include the current node, then its value is not added, but the children node can be taken or it cannot be taken, hence the summation of the maximum of both for all the children will be taken into count to compute dp2[node]. That is,
dp2[node] = tree[node] + sum(max(dp1[children1], dp2[children1]), max(dp1[children2], dp2[children2])…)
In the end, the final answer will be the maximum of dp1[root] and dp2[root].
Below is the implementation of the above approach:
Maximum sum: 25
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum sum of nodes in Binary tree such that no two are adjacent
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Maximum sum such that no two elements are adjacent
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Maximum sum in circular array such that no two elements are adjacent
- Maximum sum such that no two elements are adjacent | Set 2
- Maximum sub-sequence sum such that indices of any two adjacent elements differs at least by 3
- Maximum sum such that exactly half of the elements are selected and no two adjacent
- Maximum sum in circular array such that no two elements are adjacent | Set 2
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Remove nodes from Binary Tree such that sum of all remaining root-to-leaf paths is atleast K
- 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
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Vertex Cover Problem | Set 2 (Dynamic Programming Solution for Tree)
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Sum of nodes at maximum depth of a Binary Tree
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.