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】1731: [Usaco2005 dec]Layout 排队布局

    [题意]给定按编号顺序站成一排的牛,给定一些约束条件如两牛距离不小于或不大于某个值,求1和n的最大距离.无解输出-1,无穷解输出-2. [算法]差分约束+最短路 [题解]图中有三个约束条件,依次分析: ...

  2. ajax post请求json数据在spring-controller解析

    1.前端post请求数据: userInfo=[{"id":"5","uname":"小李","phone&q ...

  3. html+js+node实现五子棋线上对战,五子棋最简易算法

    首先附上我的github地址,https://github.com/jiangzhenfei/five,线上实例:http://47.93.103.19:5900/client/ 线上实例,你可以随意 ...

  4. Sqlmap使用教程

    sqlmap也是渗透中常用的一个注入工具,其实在注入工具方面,一个sqlmap就足够用了,只要你用的熟,秒杀各种工具,只是一个便捷性问题,sql注入另一方面就是手工党了,这个就另当别论了. 今天把我一 ...

  5. dump_stack 实现分析【转】

    转自:http://kernel.meizu.com/2017/03/18-40-19-dump_stack.html 1 简介 说起 dump_stack() ,相信从事 Linux 内核或者驱动相 ...

  6. jQuery 选中tr下面的第某个td

    1.问题描述 点击 table 中的某行 tr,获取该 tr 下的第一个 td 标签下的< input type="hidden" value="92"/ ...

  7. MyBatis 模糊查询 防止Sql注入

    #{xxx},使用的是PreparedStatement,会有类型转换,所以比较安全: ${xxx},使用字符串拼接,可以SQL注入: like查询不小心会有漏洞,正确写法如下:   Mysql:   ...

  8. udp调用connect有什么作用(转)

    原文链接如下: http://blog.csdn.net/wannew/article/details/18218619 整理一下.1:UDP中可以使用connect系统调用 2:UDP中connec ...

  9. C# 笔记——覆盖和重写

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  10. [ Python ] 基本数据类型及属性(下篇)

    1. 基本数据类型 (1) list 列表     (2) tuple 元组     (3) dict 字典     (4) set 集合 2. list 列表方法 Python 内置的一种数据类型, ...