Given an N-Ary tree containing N nodes and an array weight [ ] that denotes the weight of the nodes which can be positive or negative, the task for every node is to print the maximum sum possible by a sequence of nodes including the current node.
Input: N = 7 weight = [-8, 9, 7, -4, 5, -10, -6] N-Ary tree: -8 / \ 9 7 / \ / -4 5 -10 / -6 Output: 13 14 13 10 14 3 4 Explanations: Node -8: [-8 + 9 + 7 + 5] = 13 Node 9: [9 + 5] = 14 Node 3: [7 + (-8) + 9 + 5] = 13 Node 4: [-4 + 9 + 5] = 10 Node: [5 + 9] = 14 Node 6: [-10 + 7 + (-8) + 9 + 5] = 3 Node 7: [-6 + (-4) + 9 + 5] = 4 Input: N = 6 weight = [2, -7, -5, 8, 4, -10] N-Ary tree: 2 / \ -7 -5 / \ \ 8 4 -10 Output: 7 7 2 8 7 -8
- Apply the first DFS to store the maximum sum possible for every node by including them in a sequence with their respective successors. Store the maximum possible sum in dp1. array.
- Maximum possible value for each node in the first DFS can be obtained by:
dp1[node] += maximum(0, dp1[child1], dp1[child2], …)
- Perform the second Dfs to update the maximum sum for each node in dp1 by including them in a sequence with their ancestors also. The maximum values stored in dp2 for every node are the required answers.
- Maximum possible value for each node in the second DFS can be obtained by:
dp2[node] = dp1[node] + maximum(0, maxSumAncestors)
Best answer can be obtained by including or excluding the maximum sum possible for its ancestors
where maxSumAncestors = dp2[parent] – maximum(0, dp1[node]), i.e. including or excluding contribution of the maximum sum of the current node stored in dp1
- Iterative Segment Tree (Range Maximum Query with Node Update)
- Maximum of all subarrays of size K using Segment Tree
- Node having maximum sum of immediate children and itself in n-ary tree
- Find the maximum Even Digit Sum node in the given tree
- Get maximum left node in binary tree
- Maximum XOR with given value in the path from root to given node in the tree
- Maximum difference between node and its ancestor in Binary Tree
- Find the maximum node at a given level in a binary tree
- Find the node with maximum value in a Binary Search Tree
- Minimum valued node having maximum depth in an N-ary Tree
- Find the node with maximum value in a Binary Search Tree using recursion
- Cartesian tree from inorder traversal | Segment Tree
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Build a segment tree for N-ary rooted tree
- Segment Tree | Set 1 (Sum of given range)
- Segment Tree | Set 3 (XOR of given range)
- Persistent Segment Tree | Set 1 (Introduction)
- Two Dimensional Segment Tree | Sub-Matrix Sum
- Number of subarrays with GCD = 1 | Segment tree
- Lazy Propagation in Segment Tree | Set 2
Refer to the pictorial explanation for better understanding:
Below is the implementation of the above approach:
13 14 13 10 14 3
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.