310. Minimum Height Trees

For an undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). Given such a graph, write a function to find all the MHTs and return a list of their root labels.

Format The graph contains n nodes which are labeled from 0 to n - 1. You will be given the number n and a list of undirected edges (each edge is a pair of labels).

You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.

First let’s review some statement for tree in graph theory:

(1) A tree is an undirected graph in which any two vertices are
connected by exactly one path.

(2) Any connected graph who has n nodes with n-1 edges is a tree.

(3) The degree of a vertex of a graph is the number of edges incident
to the vertex.

(4) A leaf is a vertex of degree 1. An internal vertex is a vertex of
degree at least 2.

(5) A tree is called a rooted tree if one vertex has been designated
the root.

(6) The height of a rooted tree is the number of edges on the longest
downward path between root and a leaf.

基本方法,深度优先,求出每一个点作为root时候的height,找到最小height。
代码:

class Solution {
public:
    vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) {
        vector<int> res;
        if (n < 1) return res;
        if (n == 1) return {0};
        vector<int> heights;
        unordered_map<int, vector<int>> hash;
        for (auto e: edges) {
            hash[e.first].push_back(e.second);
            hash[e.second].push_back(e.first);
        }
        int min_h = INT_MAX;;
        for (int i = 0; i < n; i++) {
            vector<int> isVisited(n, 0);
            int h = dfs(i, hash, isVisited);
            heights.push_back(h);
            min_h = min(min_h, h);
        }
        for (int i = 0; i < n; i++) {
            if (heights[i] == min_h) res.push_back(i);
        }
        return res;
    }
    int dfs(int root, unordered_map<int, vector<int>>& hash, vector<int>& isVisited) {
        if (hash.find(root) == hash.end()) return 0;
        if (isVisited[root] > 0) return 0;
        int max_h = 0;
        isVisited[root]++;
        for (int i = 0; i < hash[root].size(); i++) {
            int temp_h = 1 + dfs(hash[root][i], hash, isVisited);
            max_h = max(max_h, temp_h);
        }
        return max_h;
    }
};

当然,像这里图的节点是有范围的整数,需要hash table,可以直接二维vector,代表adjacency matrix.
这种建立图,然后深度优先,注意避免重复访问(isVisited),的思路很常见了。这里可以运行,但会超时。

方法二:
refer to: https://leetcode.com/problems/minimum-height-trees/discuss/76055/Share-some-thoughts
基本思路是,明确每个点的degree,从每个leaf同时出发,每次走一步,最后剩下的一个或者两个点就是整个图里最长路径的中心,也就是最适合做root的点。
代码:

class Solution {
public:
    vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) {
        vector<int> res;
        if (n < 1) return res;
        if (n == 1) return {0};
        vector<vector<int>> adj(n, vector<int>());
        vector<int> degrees(n, 0);
        for (auto e: edges) {
            adj[e.first].push_back(e.second);
            adj[e.second].push_back(e.first);
            degrees[e.first]++;
            degrees[e.second]++;
        }
        queue<int> leaves;
        for (int i = 0; i < n; i++) {
            if (degrees[i] == 1) leaves.push(i);
        }
        int left = n;
        while (left > 2) {
            int num_leaves = leaves.size();
            for (int i = 0; i < num_leaves; i++) {
                int node = leaves.front();
                leaves.pop();
                left--;
                for (int j = 0; j < adj[node].size(); j++) {
                    degrees[adj[node][j]]--;
                    if (degrees[adj[node][j]] == 1) leaves.push(adj[node][j]);
                }
            }
        }
        while (!leaves.empty()) {
            res.push_back(leaves.front());
            leaves.pop();
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值