10034. Find Number of Coins to Place in Tree Nodes
You are given an undirected tree with
nnodes labeled from0ton - 1, and rooted at node0. You are given a 2D integer arrayedgesof lengthn - 1, whereedges[i] = [ai, bi]indicates that there is an edge between nodesaiandbiin the tree.You are also given a 0-indexed integer array
costof lengthn, wherecost[i]is the cost assigned to theithnode.You need to place some coins on every node of the tree. The number of coins to be placed at node
ican be calculated as:
- If size of the subtree of node
iis less than3, place1coin.- Otherwise, place an amount of coins equal to the maximum product of cost values assigned to
3distinct nodes in the subtree of nodei. If this product is negative, place0coins.Return an array
coinof sizensuch thatcoin[i]is the number of coins placed at nodei.