A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.
Given a tree of n nodes labelled from 0 to n - 1, and an array of n - 1edges where edges[i] = [ai, bi] indicates that there is an undirected edge between the two nodes ai and bi in the tree, you can choose any node of the tree as the root. When you select a node x as the root, the result tree has height h. Among all possible rooted trees, those with minimum height (i.e. min(h)) are called minimum height trees (MHTs).
Return a list of all MHTs' root labels. You can return the answer in any order.
The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
Example 1:
Input: n = 4, edges = [[1,0],[1,2],[1,3]]
Output: [1]
Explanation: As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT.
Example 2:
Input: n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
Output: [3,4]
Example 3:
Input: n = 1, edges = []
Output: [0]
Example 4:
Input: n = 2, edges = [[0,1]]
Output: [0,1]
Constraints:
1 <= n <= 2 * 104
edges.length == n - 1
0 <= ai, bi < n
ai != bi
All the pairs (ai, bi) are distinct.
The given input is guaranteed to be a tree and there will be no repeated edges.
Solution
1. Naive DFS (Timeout)
class Solution {
public:
vector<int> findMinHeightTrees(const int n, vector<vector<int>>& edges) {
vector<vector<int>> adj(n, vector<int>());
for (const auto &edge: edges) {
adj[edge[0]].push_back(edge[1]);
adj[edge[1]].push_back(edge[0]);
}
bool visited[n];
vector<int> depth(n);
int min_depth = INT_MAX;
for (int i = 0; i < n; ++i) {
memset(visited, 0, n);
depth[i] = getMaxDepth(i, adj, visited);
if (depth[i] < min_depth) min_depth = depth[i];
}
vector<int> nodes;
for (int i = 0; i < n; ++i) {
if (depth[i] == min_depth) nodes.push_back(i);
}
return nodes;
}
int getMaxDepth(int src, vector<vector<int>> &adj, bool *visited) {
visited[src] = true;
int max_depth = 0;
for (const auto e: adj[src]) {
if (visited[e]) continue;
visited[e] = true;
int d = getMaxDepth(e, adj, visited) + 1;
if (d > max_depth) max_depth = d;
}
return max_depth;
}
};
2. Topological Sort using Hash Set
class Solution {
public:
vector<int> findMinHeightTrees(const int n, vector<vector<int>>& edges) {
vector<unordered_set<int>> adj(n, unordered_set<int>());
vector<int> current, next, remove;
for (int i = 0; i < n; ++i) current.push_back(i);
for (const auto &edge: edges) {
adj[edge[0]].insert(edge[1]);
adj[edge[1]].insert(edge[0]);
}
while (current.size() > 2) {
for (const auto e: current) {
if (adj[e].size() > 1) next.push_back(e);
else remove.push_back(e);
}
for (const auto e: remove) {
int target = *adj[e].begin();
adj[target].erase(e);
}
remove.clear();
current.swap(next);
next.clear();
}
return current;
}
};
3. Topological Sort using Queue
class Solution {
public:
vector<int> findMinHeightTrees(const int n, vector<vector<int>>& edges) {
vector<vector<int>> adj(n, vector<int>());
int degree[n];
memset(degree, 0, n * sizeof(n));
for (const auto &edge: edges) {
adj[edge[0]].push_back(edge[1]);
adj[edge[1]].push_back(edge[0]);
++degree[edge[0]];
++degree[edge[1]];
}
queue<int> q;
for (int i = 0; i < n; ++i) {
if (degree[i] > 1) continue;
q.push(i);
}
int left = n;
while (left > 2) {
left -= q.size();
const int sz = q.size();
for (int i = 0; i < sz; ++i) {
const int e = q.front();
q.pop();
vector<int> &neighbor = adj[e];
for (const auto _n: neighbor) {
--degree[_n];
if (degree[_n] == 1) q.push(_n);
}
}
}
vector<int> centers;
centers.reserve(q.size());
while (!q.empty()) {
centers.push_back(q.front());
q.pop();
}
return centers;
}
};