[每日一刷] (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.
Example:
Given n = 4, edges = [[1, 0], [1, 2], [1, 3]]
0
|
1
/ \
2 3
return [1]
解题思路
本题个人的思路比较麻烦:首先对点0使用DFS,然后判断要求的点会在0点的哪个方向,然后再向那个方向移动
每次移动的时候都会有一部分的点的高度下降,而一部分的点的高度上升
代码稍微复杂了点,晚些时候再看看别人的思路好了-.-
Python:
|
|