<p>A tree is an undirected graph in which any two vertices are connected by&nbsp;<i>exactly</i>&nbsp;one path. In other words, any connected graph without simple cycles is a tree.</p>

<p>Given a tree of <code>n</code> nodes&nbsp;labelled from <code>0</code> to <code>n - 1</code>, and an array of&nbsp;<code>n - 1</code>&nbsp;<code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an undirected edge between the two nodes&nbsp;<code>a<sub>i</sub></code> and&nbsp;<code>b<sub>i</sub></code> in the tree,&nbsp;you can choose any node of the tree as the root. When you select a node <code>x</code> as the root, the result tree has height <code>h</code>. Among all possible rooted trees, those with minimum height (i.e. <code>min(h)</code>)&nbsp; are called <strong>minimum height trees</strong> (MHTs).</p>

<p>Return <em>a list of all <strong>MHTs&#39;</strong> root labels</em>.&nbsp;You can return the answer in <strong>any order</strong>.</p>

<p>The <strong>height</strong> of a rooted tree is the number of edges on the longest downward path between the root and a leaf.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/e1.jpg" style="width: 800px; height: 213px;" />
<pre>
<strong>Input:</strong> n = 4, edges = [[1,0],[1,2],[1,3]]
<strong>Output:</strong> [1]
<strong>Explanation:</strong> As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/e2.jpg" style="width: 800px; height: 321px;" />
<pre>
<strong>Input:</strong> n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
<strong>Output:</strong> [3,4]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>edges.length == n - 1</code></li>
	<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
	<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
	<li>All the pairs <code>(a<sub>i</sub>, b<sub>i</sub>)</code> are distinct.</li>
	<li>The given input is <strong>guaranteed</strong> to be a tree and there will be <strong>no repeated</strong> edges.</li>
</ul>

class Solution {
public:
    vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
        if (n == 1) return {0};

        unordered_map<int, vector<int>> graph;
        vector<int> buffer(n, 0);

        for (auto e : edges) {
            graph[e[0]].push_back(e[1]);
            graph[e[1]].push_back(e[0]);

            buffer[e[0]]++;
            buffer[e[1]]++;
        }

        queue<int> q;
        vector<int> visited(n, 0);
        for (int i = 0; i < n; i++) {
            if (buffer[i] == 1) {
                q.push(i);
                visited[i] = 1;
            }
        }

        vector<int> res;
        while (!q.empty()) {
            res.clear();
            int qn = q.size();
            
            while (qn--) {
                int r = q.front();
                q.pop();
                res.push_back(r);

                for (auto nei : graph[r]) {
                    buffer[nei]--;
                    if (!visited[nei] && buffer[nei] == 1) {
                        q.push(nei);
                        visited[nei] = 1;
                    }
                }
            }
        }

        return res;
    }









    // trim out the leaf nodes layer by layer, 
    // until we reach the core of the graph, which are the centroids nodes.
    vector<int> findMinHeightTrees1(int n, vector<vector<int>>& edges) {
        if ( n == 1 )
            return {0};
        
        // careful, Initialize the undirected graph
        vector<set<int> > adj(n);
        for ( auto edge : edges ) {                         
            adj[edge[0]].insert(edge[1]);
            adj[edge[1]].insert(edge[0]);
        }
        
        // Create first leaf layer
        vector<int> res;
        for ( int i = 0; i < n; i++ ) { 
            if ( adj[i].size() == 1 )
                res.push_back(i);
        }
        
        // BFS the graph
        while ( n > 2 ) {                                   
            n -= res.size();
            
            vector<int> newLeaves;
            for ( auto i : res ) {
                // careful, get value from set
                int j = *adj[i].begin();    
                adj[j].erase(i);

                // careful, size() == 1, not 0. 
                // Meaning this is new leaf.
                if ( adj[j].size() == 1 )   
                    newLeaves.push_back(j);
            }
            res = newLeaves;
        }
        
        // The remaining nodes are the centroids of the graph
        return res;
    }

    vector<int> findMinHeightTrees2(int n, vector<vector<int>>& edges) {
        vector<vector<int>> graph(n);
        vector<int> indegree(n, 0), ans; 
        
        // Creating an adjacency matrix for the given graph
        for(auto &e : edges){   
            graph[e[0]].push_back(e[1]);
            graph[e[1]].push_back(e[0]);
            indegree[e[0]]++;
            indegree[e[1]]++;
        }
        
        // Add all the leaf nodes to the queue
        queue<int> q;
        for(int i=0; i<n;i++){
            if(indegree[i]==1) q.push(i), indegree[i]--; 
        } 
        
        while(!q.empty()){
            int s = q.size();
            ans.clear();
            for(int i=0; i<s;i++){
                int curr = q.front(); 
                q.pop();
                ans.push_back(curr);

                // For each node, attached to the leaf niodes, we decrement the indegree 
                // i.e remove the leaf nodes connected to them. 
                // We keep on doing this until we reach the middle nodes.
                for(auto child : graph[curr]){ 
                    indegree[child]--;
                    if(indegree[child]==1) q.push(child);   
                }
            }
        }

        //If there is only 1 node in the graph, the min height is 0, with root being '0'
        if(n==1) ans.push_back(0); 
        
        return ans;
    }
};
69559