【第四周】310. Minimum Height Trees

来源:互联网 发布:3d外观设计软件 编辑:程序博客网 时间:2024/06/04 00:37

原题:

For a 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.

Example 1:

Given n = 4, edges = [[1, 0], [1, 2], [1, 3]]

    0    |    1   / \  2   3

return [1]

Example 2:

Given n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]

 0  1  2  \ | /    3    |    4    |    5

return [3, 4]

Note:

(1) According to the definition of tree on Wikipedia: “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.”

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

leetcode地址:https://leetcode.com/problems/minimum-height-trees/description/

解题思路

题目的大致意思是:any connected graph without simple cycles is a tree,给出一个无环图,找到所有高度最小的树的根节点。

一开始的思路是这样的:要找到高度最小的树,可以将任意一个节点视为根节点,然后对其进行bfs操作,遍历最后一个节点的层数即是这棵树的高度。从这个思路出发,将所有节点全部进行一遍bfs,就可以找到层数最小的节点,即为MHT的根节点。

然后代码实现,提交,TimeLimited。

审视一下上述算法的时间复杂度:对一个节点进行bfs需要n次操作,n个节点的图的时间复杂度则为o(n^2)。显然,这样的时间复杂度是不合要求的。

那么,我们肯定是漏掉了一些条件没有用上,如果加入这些条件,算法的大部分操作都是多余的。回顾一下题目的Note:“ any connected graph without simple cycles is a tree.” 这里有一个“without simple cycles“, 即任何可以转化为树的图都是无环的。而前面所使用的bfs,是不区分有环和无环的。所以现在我们得丢开bfs;看看有没有别的思路。

要找到MHT的根节点,就好像要找到一个图的”中心“位置,MHT的所有子树的高度基本上是相同的,不会出现”参差不齐“的现象。那么,如果我们一层一层的砍掉MHT的叶子层,最后剩下的节点就只有MHT的根节点了。

然后我们发现,叶子节点在图里非常容易识别:只有一条邻边。那么我们就可以一层一层去掉叶子,即去掉图中所有只有一条边的节点,同时也删掉这条边;这样一遍一遍地删除,最后剩下的一个或者两个(为什么是一到两个就不详细解释了)节点就是我们所要找的MHT的根节点了。

代码

class Solution {public:    vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) {        map<int, set<int> > graph;        set<int> res;        for (int i = 0; i < n; i++) {            set<int> edg;            graph.insert(pair<int, set<int>>(i, edg));            res.insert(i);        }        for (int i = 0; i < edges.size(); i++) {            int a = edges[i].first, b = edges[i].second;            graph[a].insert(b);            graph[b].insert(a);        }        while (res.size() > 2) {            vector<int> todos;            for (auto i = res.begin(); i != res.end(); i++) {                if (graph[*i].size() == 1) {                    todos.push_back(*i);                                 }            }            for (int i = 0; i < todos.size(); i++) {                res.erase(todos[i]);                int temp = *(graph[todos[i]].begin());                graph[temp].erase(todos[i]);            }        }        vector<int> resVec;        for (auto i = res.begin(); i != res.end(); i++) resVec.push_back(*i);        return resVec;    }    /*int bfs(int n, int k, map<int, vector<int> > graph) {        int res = 0, cur_level_num = 0;        set<int> visited;        queue<int> que;        que.push(k);        visited.insert(k);        cur_level_num = 1;        while (visited.size() < n) {            int next_level_num = 0;            for (int i = 0; i < cur_level_num; i++) {                int t = que.front();                que.pop();                for (int j = 0; j < graph[t].size(); j++) {                    if (visited.count(graph[t][j]) == 0) {                        visited.insert(graph[t][j]);                        que.push(graph[t][j]);                        next_level_num++;                    }                }            }            cur_level_num = next_level_num;            res++;        }        return res;    }*/};

总结

1、要熟悉图和树的性质。
2、要充分观察和利用条件。