DFS and postorder beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    ArrayList<Long> postorder(ArrayList<ArrayList<Integer>> arr, int i, long[] ans, int[] cost, int[] vis){\n        \n        vis[i] = 1;\n        ArrayList<Long> arli = new ArrayList<>(); \n        for(int x: arr.get(i)){\n            if(vis[x]!=1){\n            ArrayList<Long> sub = postorder(arr, x, ans, cost, vis);\n            for(long m: sub){\n                arli.add(m);\n            }}\n        }\n        arli.add((long)cost[i]);\n        if(arli.size()<3){\n            ans[i] = 1;\n            return arli;\n        }\n        else{\n            \n            Collections.sort(arli);\n            ans[i] = arli.get(arli.size()-1)*arli.get(arli.size()-3)*arli.get(arli.size()-2);\n            ans[i] = Math.max(arli.get(arli.size()-1)*arli.get(0)*arli.get(1),ans[i]);\n            if(ans[i]<0){\n                ans[i] = 0;\n            }\n        }\n        ArrayList<Long> retArray = new ArrayList<>();\n        if(arli.size()>5){\n            retArray.add(arli.get(arli.size()-1));\n            retArray.add(arli.get(arli.size()-2));\n            retArray.add(arli.get(arli.size()-3));\n            retArray.add(arli.get(0));\n            retArray.add(arli.get(1));\n            return retArray;\n        }\n        \n        return arli; \n        \n    }\n    \n    public long[] placedCoins(int[][] edges, int[] cost) {\n        \n        int n = cost.length;\n        ArrayList<ArrayList<Integer>> arr = new ArrayList<>();\n        for(int i=0;i<n;i++){\n            arr.add(new ArrayList<>());\n        }\n        for(int i=0;i<edges.length;i++){\n            arr.get(edges[i][0]).add(edges[i][1]);\n            arr.get(edges[i][1]).add(edges[i][0]);\n        }\n        int[] vis = new int[n];\n        long[] ans = new long[n];\n        postorder(arr, 0, ans, cost, vis);\n        return ans;\n    }\n}\n```
['Java']