No Ancestor Subset

Company: HPE_28sep

Difficulty: medium

Problem Statement

No Ancestor Subset Problem Description Given a tree with tree_nodes nodes, where each node i has an associated weight weight[i] (1-based indexing). The tree is rooted at node 1. A subset of nodes is called "good" if there is no pair of nodes where one node is an ancestor of the other. Find the maximum sum of weights from nodes that form a good subset. Note: A node u is an ancestor of node v if it lies on the direct path from the root to node v . Complete the function findMaximumSum in the editor with the following parameters: int tree_nodes : the number of nodes int tree_from[tree_nodes - 1] : one set of endpoints of the edges int tree_to[tree_nodes - 1] : the other set of endpoints of the edges int weight[tree_nodes] : the weights of the nodes Returns: long : the maximum sum of nodes of a good subset Examples Example 1: Input: 3 2 1 2 1 3 3 2 2 1 Output: 3 Explanation: The tree has 3 nodes. Edges are (1,2) and (1,3). Weights are weight = [2, 2, 1] for nodes 1, 2, 3 respectively. Node