<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>

struct OutNodeWithDepth {
    int node = 0;
    int depth = 0;
};

struct NodeData {
    std::vector<OutNodeWithDepth> out_nodes;
    int depth = 0;
};

class Solution {
public:
    vector<int> findMinHeightTrees(const size_t n,
                                   const vector<vector<int>>& edges) {

        // Use GNU extension for stack variable-sized array allocation.
        std::vector<NodeData> nodes_data(n);
        for (const auto& edge : edges) {
            nodes_data[edge[0]].out_nodes.push_back(
                OutNodeWithDepth{edge[1], 0});
            nodes_data[edge[1]].out_nodes.push_back(
                OutNodeWithDepth{edge[0], 0});
        }

        CalcMaxBranchDepth_r(0, n + 1, nodes_data);
        CalcNodeOverallDepth_r(0, n + 1, 0, nodes_data);

        int min_depth = std::numeric_limits<int>::max();
        for (const NodeData& node_data : nodes_data)
            min_depth = std::min(min_depth, node_data.depth);

        std::vector<int> res;
        for (size_t i = 0; i < n; ++i)
            if (nodes_data[i].depth == min_depth)
                res.push_back(i);

        return res;
    }

    int CalcMaxBranchDepth_r(const int node, const int parent_node,
                             const std::span<NodeData> nodes_data) {

        int max_depth = 0;
        for (OutNodeWithDepth& node_with_depth : nodes_data[node].out_nodes) {
            const int out_node = node_with_depth.node;
            if (out_node != parent_node) {
                const int depth =
                    CalcMaxBranchDepth_r(out_node, node, nodes_data);
                node_with_depth.depth = depth;
                if (depth > max_depth)
                    max_depth = depth;
            }
        }
        return max_depth + 1;
    }

    void CalcNodeOverallDepth_r(const int node, const int parent_node,
                                const int prev_depth,
                                const std::span<NodeData> nodes_data) {

        NodeData& node_data = nodes_data[node];

        if(node_data.out_nodes.empty())
        {
             node_data.depth = prev_depth;
            return;
        }

        // Use GNU extension for stack variable-sized array allocation.
        int max_depth_after[node_data.out_nodes.size()];
        int max_depth = prev_depth;
        for (size_t i = node_data.out_nodes.size() - 1;;) {
            max_depth_after[i] = max_depth;
            max_depth = std::max(max_depth, node_data.out_nodes[i].depth);
            if (i == 0)
                break;
            --i;
        }

        node_data.depth = max_depth;

        max_depth = prev_depth;
        for (size_t i = 0; i < node_data.out_nodes.size(); ++i) {
            const int out_node = node_data.out_nodes[i].node;
            if (out_node != parent_node) {
                const int max_depth_except_this =
                    std::max(max_depth, max_depth_after[i]);
                CalcNodeOverallDepth_r(out_node, node,
                                       max_depth_except_this + 1, nodes_data);
            }
            max_depth = std::max(max_depth, node_data.out_nodes[i].depth);
        }
    }
};
62890