Simple DFS Solution C++
Code\n\nclass Solution {\npublic:\n    vector<long long> answer;\n    vector<int> helper(int currentNode, vector<vector<int>> &adjacency, vector<int>& cost, vec
# Code\n```\nclass Solution {\npublic:\n    vector<long long> answer;\n    vector<int> helper(int currentNode, vector<vector<int>> &adjacency, vector<int>& cost, vector<bool> &visited) {\n        // check if leaf node\n        if(currentNode != 0 && adjacency[currentNode].size()==1){\n            answer[currentNode] = 1;\n            return {1, cost[currentNode], INT_MIN, INT_MIN, cost[currentNode], INT_MAX, INT_MAX};\n        }\n\n        // not leaf node \n        // add to visited\n        visited[currentNode]=true;\n\n        priority_queue<int> max_heap;\n        priority_queue<int, vector<int>, greater<int>> min_heap;\n        // loop over childrens\n        int subtree_node_count = 1;\n        max_heap.push(cost[currentNode]);\n        min_heap.push(cost[currentNode]);\n        \n        for(int i=0; i<adjacency[currentNode].size(); i++) {\n            if(visited[adjacency[currentNode][i]] == true)\n            continue;\n\n            vector<int> container = helper(adjacency[currentNode][i], adjacency, cost, visited);\n            \n            // add every subtree count of nodes \n            subtree_node_count += container[0];\n            // add every subtree cost to max heap\n            for(int j=1;j<4;j++)\n                max_heap.push(container[j]);\n            \n            for(int j=4;j<=6;j++)\n                min_heap.push(container[j]);\n                \n\n        }\n\n        vector<int> result;\n        result.push_back(subtree_node_count);\n        int x = 0;\n        while(!max_heap.empty() && x < 3)\n        {\n            //cout<<"curretNode="<<currentNode<<" ele="<<max_heap.top()<<endl;\n            result.push_back(max_heap.top());\n            max_heap.pop();\n            x++;\n        }\n        x= 0;\n        while(!min_heap.empty() && x < 3)\n        {\n            //cout<<"curretNode="<<currentNode<<" ele="<<min_heap.top()<<endl;\n            result.push_back(min_heap.top());\n            min_heap.pop();\n            x++;\n        }\n        if(subtree_node_count < 3)\n         answer[currentNode] = 1;\n        else {\n            long long ans = max(static_cast<long long>(result[1])*result[2]*result[3], static_cast<long long>(result[1])*result[4]*result[5]);\n            answer[currentNode] = ans<0 ? 0: ans;\n        }\n        \n        return result;\n    }\n    vector<long long> placedCoins(vector<vector<int>>& edges, vector<int>& cost) {\n        // create adjacency list\n        vector<vector<int>> adjacency(edges.size()+1, vector<int>());\n        answer.resize(edges.size()+1);\n        for(auto i: edges) {\n            adjacency[i[0]].push_back(i[1]);\n            adjacency[i[1]].push_back(i[0]);\n        }\n        vector<bool> visited(edges.size()+1, false);\n        helper(0, adjacency, cost, visited);\n        return answer;\n    }\n};\n```
['C++']