Lessons learned
Lesson learned:\n- Though its a tree problem, the implementation is more of a graph problem.\n- Note: The edges are given in undirected form, which mandates und
**Lesson learned**:\n- Though its a tree problem, the implementation is more of a graph problem.\n- Note: The edges are given in undirected form, which mandates undirected graph creation plus visited[].\n- maintaining two queue for positive and negative costs\n- how to choose the best k numbers out of positive and negative numbers for a product of k numbers\n- int operands of a long product will need int to long conversion by: assiging operands (either all or first operand) in long variables or upcasting to long (either all or first operand) or * 1L (in the beginning). https://stackoverflow.com/a/1494881/5649620\n\n**Code**:\n```\nclass Solution {\n    public long[] placedCoins(int[][] edges, int[] costs) {\n        List<Integer>[] tree = createTree(edges);\n        int length = edges.length + 1;\n        boolean[] visited = new boolean[length];\n        long[] ans = new long[length];\n        postOrder(0, visited, tree, 3, ans, costs);\n        return ans;\n    }\n    \n    private Queue<Integer>[] postOrder(int node, boolean[] visited, List<Integer>[] tree, int k, long[] ans, int[] costs) {\n        visited[node] = true;\n        Queue<Integer> positiveQueue = new PriorityQueue<>();\n        Queue<Integer> negativeQueue = new PriorityQueue<>((a, b) -> (b - a));\n        if (costs[node] > 0) {\n            positiveQueue.add(costs[node]);\n        } else {\n            negativeQueue.add(costs[node]);\n        }\n        for (int child : tree[node]) {\n            if (visited[child]) continue;\n            Queue<Integer>[] childCostQueues = postOrder(child, visited, tree, k, ans, costs);\n            Queue<Integer> positiveChildCostQueue = childCostQueues[0];\n            Queue<Integer> negativeChildCostQueue = childCostQueues[1];\n            for (int cost : positiveChildCostQueue) {\n                positiveQueue.add(cost);\n                if (positiveQueue.size() > k) {\n                    positiveQueue.poll();\n                }\n            }\n            for (int cost : negativeChildCostQueue) {\n                negativeQueue.add(cost);\n                if (negativeQueue.size() > k) {\n                    negativeQueue.poll();\n                }\n            }\n            \n        }\n        \n        // POPULATING ANSWER\n        ans[node] = maxProductoFKCosts(positiveQueue, negativeQueue, k);\n        \n        return new Queue[]{positiveQueue, negativeQueue};\n    }\n    \n    private List<Integer>[] createTree(int[][] edges) {\n        int length = edges.length + 1;\n        List<Integer>[] tree = new List[length];\n        for (int index = 0; index < length; ++index) {\n            tree[index] = new ArrayList<>();\n        }\n        for (int[] edge : edges) {\n            tree[edge[0]].add(edge[1]);\n            tree[edge[1]].add(edge[0]); // IMPORTANT: GIVEN EDGE IS UNDIRECCTED \n        }\n        return tree;\n    }\n    \n    private long maxProductoFKCosts(Queue<Integer> positiveQueue,  Queue<Integer> negativeQueue, int k) {\n        List<Integer> nums = new ArrayList<>();\n        for (int num : positiveQueue) {\n            nums.add(num);\n        }\n        for (int num : negativeQueue) {\n            nums.add(num);\n        }\n        Collections.sort(nums);\n        \n        int length = nums.size();\n        if (length < k) return 1;\n        \n        long val1 = (long) nums.get(length - 1) * nums.get(length - 2) * nums.get(length - 3);\n        long val2 = (long) nums.get(length - 1) * nums.get(0) * nums.get(1);\n        long max = Math.max(val1, val2);\n        return max < 0 ? 0 : max;\n    }\n    \n    \n}\n\n/*\n\n[[0,1],[0,2],[0,3],[0,4],[0,5]]\n[1,2,3,-4,-5,-6]\n[[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[0,13],[0,14],[0,15],[0,16],[0,17],[0,18],[0,19],[0,20],[0,21],[0,22],[0,23],[0,24],[0,25],[0,26],[0,27],[0,28],[0,29],[0,30],[0,31],[0,32],[0,33],[0,34],[0,35],[0,36],[0,37],[0,38],[0,39],[0,40],[0,41],[0,42],[0,43],[0,44],[0,45],[0,46],[0,47],[0,48],[0,49],[0,50],[0,51],[0,52],[0,53],[0,54],[0,55],[0,56],[0,57],[0,58],[0,59],[0,60],[0,61],[0,62],[0,63],[0,64],[0,65],[0,66],[0,67],[0,68],[0,69],[0,70],[0,71],[0,72],[0,73],[0,74],[0,75],[0,76],[0,77],[0,78],[0,79],[0,80],[0,81],[0,82],[0,83],[0,84],[0,85],[0,86],[0,87],[0,88],[0,89],[0,90],[0,91],[0,92],[0,93],[0,94],[0,95],[0,96],[0,97],[0,98],[0,99]]\n[-5959,602,-6457,7055,-1462,6347,7226,-8422,-6088,2997,-7909,6433,5217,3294,-3792,7463,8538,-3811,5009,151,5659,4458,-1702,-1877,2799,9861,-9668,-1765,2181,-8128,7046,9529,6202,-8026,6464,1345,121,1922,7274,-1227,-9914,3025,1046,-9368,-7368,6205,-6342,8091,-6732,-7620,3276,5136,6871,4823,-1885,-4005,-3974,-2725,-3845,-8508,7201,-9566,-7236,-3386,4021,6793,-8759,5066,5879,-5171,1011,1242,8536,-8405,-9646,-214,2251,-9934,-8820,6206,1006,1318,-9712,7230,5608,-4601,9185,346,3056,8913,-2454,-3445,-4295,4802,-8852,-6121,-4538,-5580,-9246,-6462]\n[[0,1],[0,2]]\n[1,2,-2]\n[[0,1],[0,2],[1,3],[1,4],[1,5],[2,6],[2,7],[2,8]]\n[1,4,2,3,5,7,8,-4,2]\n[[0,1],[0,2],[0,3],[0,4],[0,5]]\n[1,2,3,4,5,6]\n\n*/\n```
[]