|
| 1 | +{ |
| 2 | + "problem_name": "minimum_height_trees", |
| 3 | + "solution_class_name": "Solution", |
| 4 | + "problem_number": "310", |
| 5 | + "problem_title": "Minimum Height Trees", |
| 6 | + "difficulty": "Medium", |
| 7 | + "topics": "Depth-First Search, Breadth-First Search, Graph, Topological Sort", |
| 8 | + "tags": [], |
| 9 | + "readme_description": "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.\n\nGiven a tree of `n` nodes labelled from `0` to `n - 1`, and an array of `n - 1` `edges` where `edges[i] = [ai, bi]` indicates that there is an undirected edge between the two nodes `ai` and `bi` in the tree, you can choose any node of the tree as the root. When you select a node `x` as the root, the result tree has height `h`. Among all possible rooted trees, those with minimum height (i.e. `min(h)`) are called **minimum height trees** (MHTs).\n\nReturn *a list of all **MHTs'** root labels*. You can return the answer in **any order**.\n\nThe **height** of a rooted tree is the number of edges on the longest downward path between the root and a leaf.", |
| 10 | + "readme_examples": [ |
| 11 | + { |
| 12 | + "content": "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/09/01/e1.jpg\" style=\"width: 800px; height: 213px;\" />\n\n```\nInput: n = 4, edges = [[1,0],[1,2],[1,3]]\nOutput: [1]\nExplanation: As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT.\n```" |
| 13 | + }, |
| 14 | + { |
| 15 | + "content": "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/09/01/e2.jpg\" style=\"width: 800px; height: 321px;\" />\n\n```\nInput: n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]\nOutput: [3,4]\n```" |
| 16 | + } |
| 17 | + ], |
| 18 | + "readme_constraints": "- `1 <= n <= 2 * 10^4`\n- `edges.length == n - 1`\n- `0 <= ai, bi < n`\n- `ai != bi`\n- All the pairs `(ai, bi)` are distinct.\n- The given input is **guaranteed** to be a tree and there will be **no repeated** edges.", |
| 19 | + "readme_additional": "", |
| 20 | + "solution_imports": "", |
| 21 | + "solution_methods": [ |
| 22 | + { |
| 23 | + "name": "find_min_height_trees", |
| 24 | + "parameters": "n: int, edges: list[list[int]]", |
| 25 | + "return_type": "list[int]", |
| 26 | + "dummy_return": "[]" |
| 27 | + } |
| 28 | + ], |
| 29 | + "test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution", |
| 30 | + "test_class_name": "MinimumHeightTrees", |
| 31 | + "test_helper_methods": [ |
| 32 | + { "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" } |
| 33 | + ], |
| 34 | + "test_methods": [ |
| 35 | + { |
| 36 | + "name": "test_find_min_height_trees", |
| 37 | + "parametrize": "n, edges, expected", |
| 38 | + "parametrize_typed": "n: int, edges: list[list[int]], expected: list[int]", |
| 39 | + "test_cases": "[(4, [[1,0],[1,2],[1,3]], [1]), (6, [[3,0],[3,1],[3,2],[3,4],[5,4]], [3,4]), (1, [], [0])]", |
| 40 | + "body": "result = self.solution.find_min_height_trees(n, edges)\nassert sorted(result) == sorted(expected)" |
| 41 | + } |
| 42 | + ], |
| 43 | + "playground_imports": "from solution import Solution", |
| 44 | + "playground_test_case": "# Example test case\nn = 4\nedges = [[1,0],[1,2],[1,3]]\nexpected = [1]", |
| 45 | + "playground_execution": "result = Solution().find_min_height_trees(n, edges)\nresult", |
| 46 | + "playground_assertion": "assert sorted(result) == sorted(expected)" |
| 47 | +} |
0 commit comments