You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0300-0399/0310.Minimum Height Trees/README_EN.md
+63-15
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,13 @@
44
44
45
45
## Solutions
46
46
47
-
### Solution 1
47
+
### Solution 1: Topological Sorting
48
+
49
+
If the tree only has one node, then this node is the root of the minimum height tree. We can directly return this node.
50
+
51
+
If the tree has multiple nodes, there must be leaf nodes. A leaf node is a node that only has one adjacent node. We can use topological sorting to peel off the leaf nodes from the outside to the inside. When we reach the last layer, the remaining nodes are the root nodes of the minimum height tree.
52
+
53
+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the number of nodes.
0 commit comments