Simple Post Order traversal and priority queue of size 3.
Intuition\nPost Order traversal and priority queue of size 3.\n\n# Approach\nPost Order traversal and priority queue of size 3.\n\n# Complexity\n- Time complexi
# Intuition\nPost Order traversal and priority queue of size 3.\n\n# Approach\nPost Order traversal and priority queue of size 3.\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 {\npublic:\n    \n    void postorder(vector<vector<int>>& tree, int ind, vector<int>& cost, priority_queue <long long, vector<long long>>* pq, priority_queue <long long, vector<long long>>* pqn, vector<long long> &ans)\n    {\n        visit[ind] = true;\n        \n        for(int j = 0; j < tree[ind].size(); j++)\n        {\n            if(visit[tree[ind][j]])\n                continue;\n            \n            postorder(tree, tree[ind][j], cost, pq, pqn, ans);\n            \n            while(pq[tree[ind][j]].size() > 0)\n            {\n                pq[ind].push(pq[tree[ind][j]].top());\n                pq[tree[ind][j]].pop();\n                \n                \n            }\n            \n            while(pqn[tree[ind][j]].size() > 0)\n            {\n                pqn[ind].push(pqn[tree[ind][j]].top());\n                pqn[tree[ind][j]].pop();\n            }\n            \n            \n        }\n        //cout << ind << endl;\n        if(cost[ind] > 0)\n            pq[ind].push(cost[ind]);\n        else\n            pqn[ind].push(abs(cost[ind]));\n        \n        if(pq[ind].size()+pqn[ind].size() < 3)\n        {\n            ans[ind] = 1;\n        }\n        /*else if(pq[ind].size()+pqn[ind].size() == 3)\n        {\n            if(pqn[ind].size() == 2)\n            {\n                long long a = pqn[ind].top();\n                pqn.pop();\n                long long b = pqn[ind].top();\n                pqn.pop();\n                \n                ans[ind] = a*b*pq[ind].top();\n            }\n            else\n            {\n                ans[ind] = 0;\n            }\n        }*/\n        else\n        {\n            //long long pos[3];\n            //long long neg[3];\n            \n            vector<long long>pos;\n            vector<long long>neg;\n            \n            //memset(pos,0,sizeof(pos));\n            //memset(neg,0,sizeof(neg));\n            \n            int ct = 0;\n            \n            while(pq[ind].size() > 0 && ct < 3)\n            {\n                //pos[ct] = pq[ind].top();\n                pos.push_back(pq[ind].top());\n                pq[ind].pop();\n                ct++;\n            }\n            \n            while(pq[ind].size() > 0)\n                pq[ind].pop();\n            \n            ct = 0;\n            \n            while(pqn[ind].size() > 0 && ct < 3)\n            {\n                //neg[ct] = pqn[ind].top();\n                neg.push_back(pqn[ind].top());\n                pqn[ind].pop();\n                ct++;\n            }\n            \n            while(pqn[ind].size() > 0)\n                pqn[ind].pop();\n            \n            \n            if(pos.size() == 0 && neg.size() == 3)\n            {\n                //cout << "anand" << endl;\n                ans[ind] = 0;\n            }\n            else if(neg.size() == 0 && pos.size() == 3)\n            {\n                //cout << "anand1" << endl;\n                ans[ind] = pos[0]*pos[1]*pos[2];\n            }\n            else if(pos.size() <= 2 && neg.size() >= 2)\n            {\n                //cout << "anand2" << endl;\n                ans[ind] = pos[0]*neg[0]*neg[1];\n            }\n            else if(pos.size() >= 3 && neg.size() >= 2)\n            {\n                //cout << "anand3" << endl;\n                long long a1 = pos[0]*pos[1]*pos[2];\n                \n                long long b1 = pos[0]*neg[0]*neg[1];\n                \n                ans[ind] = max(a1,b1);\n            }\n            else if(pos.size() >= 3)\n            {\n                //cout << "anand4" << endl;\n                ans[ind] = pos[0]*pos[1]*pos[2];\n            }\n            \n            \n            for(int i = 0; i < pos.size(); i++)\n            {\n                pq[ind].push(pos[i]);\n            }\n            \n            for(int i = 0; i < neg.size(); i++)\n            {\n                pqn[ind].push(neg[i]);\n            }\n        }\n        \n    }\n    \n    vector<long long> placedCoins(vector<vector<int>>& edges, vector<int>& cost) {\n        \n        vector<vector<int>>tree(cost.size());\n        vector<long long> ans(cost.size());\n        \n        priority_queue <long long, vector<long long>> pq[cost.size()]; \n        priority_queue <long long, vector<long long>> pqn[cost.size()]; \n        \n        memset(visit,false,sizeof(visit));\n        \n        for(int i = 0; i < edges.size(); i++)\n        {\n            tree[edges[i][0]].push_back(edges[i][1]);\n            tree[edges[i][1]].push_back(edges[i][0]);\n        }\n        \n        postorder(tree,0,cost,pq,pqn,ans);\n        \n        return ans;\n        \n    }\n    \n    bool visit[20001];\n    \n    \n};\n```
['C++']