<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:
    int dfs(vector<vector<int> > &graph, vector<bool> &visited, int i, int &result, int& node){

        visited[i] = true;
        int max1 = -1, max2 = -1, len;
        for( auto nbr : graph[i] ){
            if( !visited[nbr] ){
                len = 1+dfs(graph, visited, nbr, result, node);
                if( len > max1 ){
                    max2 = max1;
                    max1 = len;
                }
                else if( len > max2 ){
                    max2 = len;
                }
            }
        }

        if( result < max1+max2 ){
            result = max1+max2;
            node = i;
        }

        return max1;
    }

    void dfs2(vector<vector<int> > &graph, vector<bool> &visited, int i, vector<int> &path, vector<vector<int> > &paths){

        visited[i] = true;
        path.push_back(i);
        int count = 0;
        for( auto nbr : graph[i] ){
            if( !visited[nbr] ){
               dfs2(graph, visited, nbr, path, paths);
               count++;
            }
        }
        if( count == 0 )
            paths.push_back(path);
        path.pop_back();
        return;
    }
    vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
        vector<vector<int> > graph(n);

        if( n == 1 )
            return {0};
        if( n ==2 )
            return {0,1};


        for( int i = 0; i < n-1; i++){
            graph[edges[i][1]].push_back(edges[i][0]);
            graph[edges[i][0]].push_back(edges[i][1]);
        }

        vector<bool> visited(n,0);
        int result = INT_MIN, node = -1;
        dfs(graph, visited, 0, result, node);
        cout<<node<<endl;
        visited.assign(n,0);
        vector<int> path;
        vector<vector<int> > paths;

        dfs2(graph, visited, node, path, paths);
        
        int maxlen1 = 0, maxlen2 = 0, idx1 = -1, idx2 = -1 ;
        for( int i = 0; i < paths.size(); i++){
            if( paths[i].size() > maxlen1 ){
                idx1 = i;
                maxlen1 = paths[i].size();
            }
        }

        // cout<<idx1<<" "<<idx2<<endl;
        path = {};
        for( int i = paths[idx1].size()-1; i >= 0; i--)
            path.push_back(paths[idx1][i]);

        set<int> s(path.begin(), path.end());

        for( int i = 0; i < paths.size(); i++){
            if( i != idx1 ){
                if(paths[i].size() > maxlen2 ){
                    bool flag = true;
                    for( int j = 1; j < paths[i].size() && flag; j++){
                        if( s.find(paths[i][j]) != s.end() )
                            flag = false;
                    }

                    if( flag ){
                        maxlen2 = paths[i].size();
                        idx2 = i;
                    }
                }
            }
        }

        if( idx2!=-1){
        for( int i = 1; i < paths[idx2].size(); i++)
            path.push_back(paths[idx2][i]);
        }
        
        // for( int i = 0; i < path.size(); i++ )
        //     cout<<path[i]<<" ";

        if( path.size()%2 )
            return {path[path.size()/2]};
        

        return {path[(path.size()-1)/2],path[path.size()/2]};

    }
};
76228