题目

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.Example 1 :Input: n = 4, edges = [[1, 0], [1, 2], [1, 3]]0|1/ \2   3 Output: [1]
Example 2 :Input: n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]0  1  2\ | /3|4|5 Output: [3, 4]
Note: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.”
The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.

在无向图的生成树中,我们可以指定任何一个节点为这棵树的根节点。现在要求在这样一棵生成树中,找到生成树的高度最低的所有根节点。

其实,决定一棵树的高度往往是树中的最长路径,只有我们选择最长路径的中间点才能够尽可能减少树的高度。那么我们如何找到所有的中间节点呢?

当出发点只有两个时,我们知道中间节点就是从出发点同时出发,当二者相遇或者二者只间隔一个位置是所在的点就是两个出发点的重点。那么多个出发点也是同样的道理,每个人从各个出发点出发,最终相遇的点就是我们所要找的中间点。

这题的思路有些类似于拓扑排序,每次我们都会去除所有入度为0的点,因为这些点肯定是叶节点。然后不停的往中间走,直到剩下最后一个叶节点或是最后两个叶节点。

  0|1/ \
2   3

这个图中删除所有入度为0的点就只剩下1,因此我们知道1一定就是我们所要求的根节点

思路一:图论

这一种解法着重强调了利用图论中的数据结构来解决问题。这里我们采用图论中的邻接表来存储图中的点和边。然后利用邻接表的相关属性来判断当前节点是否是叶节点。

    public List<Integer> findMinHeightTrees(int n, int[][] edges) {if(n==1) return Collections.singletonList(0);//初始化邻接表List<Set<Integer>> adj = new ArrayList<Set<Integer>>();for(int i = 0 ; i<n ; i++) {adj.add(new HashSet<Integer>());}for(int[] edge : edges) {adj.get(edge[0]).add(edge[1]);adj.get(edge[1]).add(edge[0]);}List<Integer> leaves = new ArrayList<Integer>();for(int i = 0 ; i<adj.size() ; i++) {if(adj.get(i).size() == 1) {leaves.add(i);}}while(n > 2) {n -= leaves.size();List<Integer> newLeaves = new ArrayList<>();for (int i : leaves) {int j = adj.get(i).iterator().next();adj.get(j).remove(i);if (adj.get(j).size() == 1) newLeaves.add(j);}leaves = newLeaves;}return leaves;}

思路二:简化数据结构

这里使用degree数组存储每个顶点的度数,即连接的变数。度数为一的顶点就是叶节点。再用connected存储每个顶点所连接的所有边的异或值。这里使用异或的原因是对同一个值进行两次异或即可以回到最初值。

    public List<Integer> findMinHeightTrees2(int n, int[][] edges) {if(n==1) return Collections.singletonList(0);int[] connected = new int[n];int[] degree = new int[n];for(int[] edge : edges) {int v1 = edge[0];int v2 = edge[1];connected[v1] ^= v2;connected[v2] ^= v1;degree[v1]++;degree[v2]++;}LinkedList<Integer> queue = new LinkedList<Integer>();for(int i = 0 ; i<degree.length ; i++) {if(degree[i] == 1) {queue.offer(i);}}while(n > 2 && !queue.isEmpty()) {int size = queue.size();for(int i = 0 ; i<size ; i++) {int v = queue.poll();n--;int v1 = connected[v];connected[v1] ^= v;degree[v1]--;if(degree[v1] == 1) {queue.add(v1);}}    }List<Integer> result = new ArrayList<Integer>();result.addAll(queue);return result;}


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

leetcode310. Minimum Height Trees相关推荐

  1. 树的最小高度 Minimum Height Trees

    2019独角兽企业重金招聘Python工程师标准>>> 问题: For a undirected graph with tree characteristics, we can ch ...

  2. leetcode 310. Minimum Height Trees | 310. 最小高度树(图的邻接矩阵DFS / 拓扑排序)

    题目 https://leetcode.com/problems/minimum-height-trees/ 题解 方法1:图的邻接矩阵 DFS(超时) 我一想,这不就是个图嘛,于是随手敲出一个 DF ...

  3. LeetCode Minimum Height Trees(拓扑排序)

    问题:给出一个图,要求确定树的根,使得树的高度最小. 思路:先求出结点的度.然后将度数为1的入队列.在出队列时,将其邻接结点的度数减1,如果度数为1,则放入队列.直接剩余的结点数小于等于2 具体代码参 ...

  4. 310. Minimum Height Trees

    输入:包含n个节点的无向图.n:表示从0到n-1,n个节点.edges:int数组,是从一个节点到另外一个节点.但是没有方向. 输出:以哪些节点为根节点,具有最小高度的树,返回这些根节点. 规则:一个 ...

  5. LeetCode 310. Minimum Height Trees

    文章目录 知识点 结果 菜鸡的DFS+记忆化 网友的BFS"剥洋葱" 实现 菜鸡的DFS+记忆化 代码 反思 网友的BFS"剥洋葱" 代码 反思 知识点 图的遍 ...

  6. 310. Minimum Height Trees 【Medium】 树

    题目:给出一个树,求出所有作为根节点时树的高度最小的节点 思路:用剪枝的办法,每次剪掉叶子节点,最终剩下一个或者两个节点时结束. 原理:一个节点的树的高度为该节点到最远的节点的距离,且此最远节点必为叶 ...

  7. Lecture 16 Minimum Spanning Trees

  8. HDU 6691 Minimum Spanning Trees

    题目 题意: 对于一个n个点的图,每对点 u , v u,v u,v间有 p 0 p_0 p0​的几率没有边, p 1 p_1 p1​的几率有权值为1的边, p 2 p_2 p2​-边权 < = ...

  9. 继续过中等难度.0309

      .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Mediu ...

最新文章

  1. 第二章:深入C#数据类型
  2. Jzoj3907 蜀传之单刀赴会(梦回三国系列)
  3. 管理者如何提升下属执行力---视频学习记录
  4. Mac Sublime Vim模式 方向键无法长按
  5. java写便签_如何编写一个便签程序(用Java语言编写)
  6. C/C++中NULL指针
  7. 分步表单如何实现 html_HTML表格入门的分步指南
  8. 钉钉怎么查看收到的文件 钉钉查看文件的方法
  9. matplotlib绘制图形
  10. Spring 通知和顾问进行增强
  11. java如何制作简单的数组_【数据结构与算法】Java制作一个简单数组类
  12. 合伙人的重要性超过了商业模式和行业选择(转)
  13. spring源码:实例化bean的准备工作
  14. 实训-利用HTML+CSS制作某米官网首页
  15. CCS软件报错“unresolved symbol remain”
  16. 提升企业网站用户体验 你不可不知的秘诀
  17. File类,字节字符输入输出流,缓冲流,标准流,对象序列化流
  18. Keil5-MDK 使用编译步骤及异常与修改(生成axf文件和bin文件)
  19. 计算机基础学到了哪些知识,计算机基础学习心得
  20. 西门子S7-1200PLC堆栈程序 在使用西门子1200PLC时候发现,系统没有自带的堆栈功能块,不能实现数据的先进先出后进后出功能

热门文章

  1. 《Core Data应用开发实践指南》一1.3 创建Grocery Dude项目
  2. Mysqli的批量CRUD数据
  3. 第6章-MapReduce的工作机制-笔记
  4. 我的jquery之路
  5. 分享一套超棒的iOS “空状态” (empty state) 界面UI设计
  6. ASP.NET中的加密方法介绍
  7. 使用粘性布局实现tab滑动后置顶
  8. break 和continue在循环中起到的作用
  9. H5拍照、预览、压缩、上传采坑记录
  10. Spring 4 使用Freemarker模板发送邮件添加附件