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 = 4edges = [[1, 0], [1, 2], [1, 3]]

        |

       / \
         

return [1]

Example 2:

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

      \ | /

        |

        |
        

return [3, 4]

思路:找到这棵树中所有度为1的节点,它们是最外层的节点。从树中移除这些节点,重复这个操作直到树中的节点数小于3。结果可能是1个节点或者两个节点,这取决于树中最长路径的长度(奇偶性)。时间复杂度O(N)。

class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) {
if (n == ) return vector<int>(, );
vector<unordered_set<int> > tree(n, unordered_set<int>());
for (int i = ; i < edges.size(); i++) {
tree[edges[i].first].insert(edges[i].second);
tree[edges[i].second].insert(edges[i].first);
}
vector<int> leaves;
for (int i = ; i < n; i++)
if (tree[i].size() == ) leaves.push_back(i);
while (n > ) {
vector<int> newLeaves;
for (int i = ; i < leaves.size(); i++)
for (auto j : tree[leaves[i]]) {
tree[j].erase(leaves[i]);
if (tree[j].size() == ) newLeaves.push_back(j);
}
n -= leaves.size();
leaves = newLeaves;
}
return leaves;
}
};

Minimum Height Trees -- LeetCode的更多相关文章

  1. [LeetCode] Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  2. LeetCode Minimum Height Trees

    原题链接在这里:https://leetcode.com/problems/minimum-height-trees/ 题目: For a undirected graph with tree cha ...

  3. [LeetCode] 310. Minimum Height Trees 解题思路

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  4. [LeetCode] 310. Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  5. 【LeetCode】310. Minimum Height Trees 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 相似题目 参考资料 日期 题目地址:http ...

  6. leetcode@ [310] Minimum Height Trees

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  7. [Swift]LeetCode310. 最小高度树 | Minimum Height Trees

    For an undirected graph with tree characteristics, we can choose any node as the root. The result gr ...

  8. Minimum Height Trees

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  9. 310. Minimum Height Trees

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

随机推荐

  1. 【BZOJ】1188 [HNOI2007]分裂游戏

    [算法]博弈论 [题解] 我们的目的是把游戏拆分成互不影响的子游戏,考虑游戏内的转移. 如果把每堆视为子游戏,游戏之间会相互影响,不成立. 将每堆的一个石子视为子游戏,其产生的石子都在同一个子游戏中. ...

  2. HDU - 5327 Olympiad(一维前缀和)

    Olympiad Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem ...

  3. 6、MySQL索引种类

    1.普通索引 这是最基本的索引,它没有任何限制,比如上文中为title字段创建的索引就是一个普通索引,MyIASM中默认的BTREE类型的索引,也是我们大多数情况下用到的索引. –直接创建索引 CRE ...

  4. Python作业模拟登陆(第一周)

    模拟登陆:1. 用户输入帐号密码进行登陆2. 用户信息保存在文件内3. 用户密码输入错误三次后锁定用户 思路: 1. 用户名密码文件为passwd,锁定用户文件为lock 2. 用户输入账号密码采用i ...

  5. mssql手工注入2

    --+ 先说一些函数的说明: substring(str,start,len) 截取字符串的作用,第一个参数为要截取的字符串,第二个参数为从哪里开始截取,第三个参数为截取的长度 ascii(char) ...

  6. docker使用小记

    查看当前镜像:docker images 运行一个简单的镜像:docker run hello-world 拉取一个远程docker:docker pull centos docker中安装nginx ...

  7. k8s取节点内docker中的日志

    Kubernetes(k8s)是Google开源的容器集群管理系统(谷歌内部:Borg).在Docker技术的基础上,为容器化的应用提供部署运行.资源调度.服务发现和动态伸缩等一系列完整功能,提高了大 ...

  8. c json实战引擎四 , 最后❤跳跃

    引言  - 以前那些系列 长活短说, 写的最终 scjson 纯c跨平台引擎, 希望在合适场景中替代老的csjon引擎, 速度更快, 更轻巧. 下面也是算一个系列吧. 从cjson 中得到灵感, 外加 ...

  9. 深入浅出Node.js(一) - 初识Node.js

    1.Node.js将Javascript解决不确定性所使用的事件驱动方式引入了进来,因为JS是一门事件驱动的语言,旨在能够对外界的事件作出响应; 2.Node.js中,所有的有关异步的操作,都在同步操 ...

  10. 改变ASPxpivotgridview弹出的prefilter的标题

    说是要给变标题,再网上找了很久的资料,基本上属于一无所获,后来在官网上看到一个技术支持用vb写的,说是要本地化什么的,个人技术有限不是太懂 后来干脆就直接注册个账号,发问了,好歹等到了晚上十点左右,有 ...