leetcode Minimum Height Trees - 细语呢喃
leetcode Minimum Height Trees 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 0 to n edges You can assume that no duplicate edges will appear in edges [0, 1] edges Python , edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]] Python 1 2 3 4 5 6 7 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.” 题意: 思路: Code python Python # leetcode Minimum Height Trees # http://www.hrwhisper.me/leetcode-minimum-height-trees/ class Solution(object): def findMinHeightTrees(self, n, edges): """ :type n: int :type edges:Read full article from leetcode Minimum Height Trees - 细语呢喃
No comments:
Post a Comment