Track 3 largest and 2 smallest value for each subtree | dfs | 
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
// lc
#define vi vector<ll>
#define vvii vector<vi>
#define vpii vector<pair<int,int>>
#define pii  pair<int,int>
#define ll long long int
#define vs vector<string>
#define mii map<int, int>
#define umii unordered_map<int, int> 
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define sall(x) sort(x.begin(), x.end());
#define rall(x) sort(x.rbegin(), x.rend())
#define pb push_back 
#define in insert
#define ff first
#define ss second
#define rep(i, a, b) for (int i = a; i < b; i++)
#define all(x) x.begin(), x.end()
#define sall(x) sort(x.begin(), x.end());
#define rall(x) sort(x.rbegin(), x.rend())
#define MAX(x) *max_element(x.begin(), x.end())
#define MIN(x) *min_element(x.begin(), x.end())
#define SUM(X) accumulate(X.begin(), X.end(), 0LL)
#define rev(a) reverse(a.begin(), a.end());
const int mod = 1e9 + 7;


class Solution {
public:
    vector<long long> placedCoins(vector<vector<int>>& edges, vector<int>& cost) {
        int n = cost.size();
        vvii adj(n+1);
        for(auto it: edges) {
            adj[it[0]].pb(it[1]);
            adj[it[1]].pb(it[0]);
        }
        vi first(n+1,-1e9),second(n+1,-1e9),third(n+1,-1e9), firstMin(n+1,1e9), secondMin(n+1,1e9);
        auto dfs = [&](int node, int par, auto &&self)->void{
            first[node] = cost[node];
            firstMin[node] = cost[node];
            for(auto it: adj[node]) {
                if(it != par) {
                    self(it, node, self);

                    //calculating largest, secondLargest, thirdLargest

                    if(first[it] >= first[node]) {
                       third[node] = second[node];
                       second[node] = first[node];
                       first[node] = first[it];
                    } else if(first[it] >= second[node]) {
                        third[node] = second[node];
                        second[node] = first[it];
                    } else if(first[it] >= third[node]) {
                        third[node] = first[it];
                    }

                    if(second[it] >= first[node]) {
                       third[node] = second[node];
                       second[node] = first[node];
                       first[node] = second[it];
                    } else if(second[it] >= second[node]) {
                        third[node] = second[node];
                        second[node] = second[it];
                    } else if(second[it] >= third[node]) {
                        third[node] = second[it];
                    }

                    if(third[it] >= first[node]) {
                       third[node] = second[node];
                       second[node] = first[node];
                       first[node] = third[it];
                    } else if(third[it] >= second[node]) {
                        third[node] = second[node];
                        second[node] = third[it];
                    } else if(third[it] >= third[node]) {
                        third[node] = third[it];
                    }

                    //calculating smallest and secondSmallest Minimum

                    if(firstMin[it] <= firstMin[node]) { 
                       secondMin[node] = firstMin[node];
                       firstMin[node] = firstMin[it];
                    } else if(firstMin[it] <= secondMin[node]) { 
                        secondMin[node] = firstMin[it];
                    } 

                     if(secondMin[it] <= firstMin[node]) { 
                       secondMin[node] = firstMin[node];
                       firstMin[node] = secondMin[it];
                    } else if(secondMin[it] <= secondMin[node]) { 
                        secondMin[node] = secondMin[it];
                    } 
                }
            }
        };
        dfs(0,-1,dfs);
        vi ans(n);
        rall(cost);
        // cout<<cost[0]<<" "<<cost[1]<<" "<<cost[2]<<endl;
        rep(i,0,n) {
            if(first[i]== -1e9 or second[i]== -1e9 or third[i]== -1e9) {
                ans[i] = 1;
            } else { 
                ans[i] = max(0LL, first[i]*1LL*second[i]*1LL*third[i]);
                // cout<<i<<" : "<<first[i]<<" "<<second[i]<<" "<<third[i]<<" "<<firstMin[i]<<" "<<secondMin[i]<<endl;
                if(firstMin[i]<0 and secondMin[i]<0) {
                    ans[i] = max(ans[i], max(0LL, first[i]*1LL*firstMin[i]*1LL*secondMin[i]));
                }
            }
        }
        return ans;
    }
};
```
['C++']