摘要:现在要求在这样一棵生成树中,找到生成树的高度最低的所有根节点。然后利用邻接表的相关属性来判断当前节点是否是叶节点。度数为一的顶点就是叶节点。这里使用异或的原因是对同一个值进行两次异或即可以回到最初值。
题目
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思路二:简化数据结构findMinHeightTrees(int n, int[][] edges) { if(n==1) return Collections.singletonList(0); //初始化邻接表 List > adj = new ArrayList >(); for(int i = 0 ; i ()); } for(int[] edge : edges) { adj.get(edge[0]).add(edge[1]); adj.get(edge[1]).add(edge[0]); } List leaves = new ArrayList (); for(int i = 0 ; i 2) { n -= leaves.size(); List 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 ListfindMinHeightTrees2(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 queue = new LinkedList (); for(int i = 0 ; i 2 && !queue.isEmpty()) { int size = queue.size(); for(int i = 0 ; i result = new ArrayList (); result.addAll(queue); return result; }
想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/72303.html
摘要:可以从头边同时进行,查看叶子节点并加入到叶子节点链表遍历一遍后,叶子节点链表为。将叶子节点保存下来。这个时候就会有第二层叶子节点那些在列表当中为的点,用同样的方法进行剥除。最后留在叶子节点里面的点即可以为根。 题目: For a undirected graph with tree characteristics, we can choose any node as the root...
摘要:题目链接图的题,和差不多。来解,每次放入只有一个的现在的。然后直到只剩最上面一层。注意考虑单独的和别的不相连。比如这种情况,就有三个点都不和别的相连。还有的时候就要返回。 Minimum Height Trees 题目链接:https://leetcode.com/problems... 图的题,和course schedule差不多。bfs来解,每次放入只有一个edge的node(现...
Problem You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-negative 2D map, in this map: 0 represents the obstacle cant be reached.1 represents the ground ...
Problem Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required. Example 1: Input: [[0, 30],[5,...
Unique Binary Search Trees Problem Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Given n = 3, there are a total of 5 unique BSTs. 1 3 3...
阅读 3207·2021-11-08 13:21
阅读 1198·2021-08-12 13:28
阅读 1408·2019-08-30 14:23
阅读 1927·2019-08-30 11:09
阅读 843·2019-08-29 13:22
阅读 2687·2019-08-29 13:12
阅读 2550·2019-08-26 17:04
阅读 2253·2019-08-26 13:22