<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<vector<int>> adj;
    vector<pair<int, int>> child;
    vector<pair<int, int>> second_child;
    vector<pair<int, int>> maxlen1;
    vector<int> maxlen2;

    int compute_child(int cur, int prev=-1) {
        int len = 0;
        for (int neighbor : adj[cur]) {
            if (neighbor != prev) {
                int child_val = compute_child(neighbor, cur);
                len = max(len, 1 + child_val);
                if (1 + child_val >= child[cur].first) {
                    second_child[cur] = child[cur];
                    child[cur] = {1 + child_val, neighbor};
                } else if (1 + child_val > second_child[cur].first) {
                    second_child[cur] = {1 + child_val, neighbor};
                }
            }
        }
        return len;
    }

    void compute_maxlen(int cur, int prev) {
        auto [maxlen, direction] = child[cur];
        if (1 + maxlen1[prev].first > maxlen) {
            if (maxlen1[prev].second != cur) {
                maxlen = 1 + maxlen1[prev].first;
                direction = prev;
            } else if (1 + maxlen2[prev] > maxlen) {
                maxlen = 1 + maxlen2[prev];
                direction = prev;
            }
        }
        maxlen1[cur] = {maxlen, direction};

        if (direction == prev) {
            maxlen2[cur] = child[cur].first;
        } else {
            int new_maxlen = maxlen1[prev].second != cur ? 1 + maxlen1[prev].first : 1 + maxlen2[prev];
            for (int neighbor : adj[cur]) {
                if (neighbor != prev && neighbor != direction) {
                    new_maxlen = max(new_maxlen, 1 + child[neighbor].first);
                }
            }
            maxlen2[cur] = new_maxlen;
        }

        for (int neighbor : adj[cur]) {
            if (neighbor != prev) {
                compute_maxlen(neighbor, cur);
            }
        }
    }

    vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
        adj.resize(n);
        child.resize(n);
        second_child.resize(n);
        maxlen1.resize(n);
        maxlen2.resize(n);
        for (auto& edge : edges) {
            int a = edge[0], b = edge[1];
            adj[a].push_back(b);
            adj[b].push_back(a);
        }

        compute_child(0);
        for (int i = 0; i < n; ++i) {
            cout << child[i].first << ' ' << child[i].second << ' ' << second_child[i].first << ' ' << second_child[i].second << '\n';
        }
        maxlen1[0] = child[0];
        maxlen2[0] = second_child[0].first;
        for (int neighbor : adj[0]) {
            compute_maxlen(neighbor, 0);
        }

        for (int i = 0; i < n; ++i) {
            cout << maxlen1[i].first << ' ' << maxlen1[i].second << ' ' << maxlen2[i] << '\n';
        }

        int diameter = 0;
        for (int i = 0; i < n; ++i) {
            diameter = max(diameter, maxlen1[i].first);
        }
        vector<int> ans;
        for (int i = 0; i < n; ++i) {
            if (maxlen1[i].first == (diameter + 1) / 2) {
                ans.push_back(i);
            }
        }
        return ans;
    }
};
60465