|
| 1 | +/** |
| 2 | + * [310] Minimum Height Trees |
| 3 | + * |
| 4 | + * 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. |
| 5 | + * |
| 6 | + * Format<br /> |
| 7 | + * 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). |
| 8 | + * |
| 9 | + * 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. |
| 10 | + * |
| 11 | + * Example 1 : |
| 12 | + * |
| 13 | + * |
| 14 | + * Input: n = 4, edges = [[1, 0], [1, 2], [1, 3]] |
| 15 | + * |
| 16 | + * 0 |
| 17 | + * | |
| 18 | + * 1 |
| 19 | + * / \ |
| 20 | + * 2 3 |
| 21 | + * |
| 22 | + * Output: [1] |
| 23 | + * |
| 24 | + * |
| 25 | + * Example 2 : |
| 26 | + * |
| 27 | + * |
| 28 | + * Input: n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]] |
| 29 | + * |
| 30 | + * 0 1 2 |
| 31 | + * \ | / |
| 32 | + * 3 |
| 33 | + * | |
| 34 | + * 4 |
| 35 | + * | |
| 36 | + * 5 |
| 37 | + * |
| 38 | + * Output: [3, 4] |
| 39 | + * |
| 40 | + * Note: |
| 41 | + * |
| 42 | + * |
| 43 | + * According to the <a href="https://en.wikipedia.org/wiki/Tree_(graph_theory)" target="_blank">definition of tree on Wikipedia</a>: “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.” |
| 44 | + * The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf. |
| 45 | + * |
| 46 | + * |
| 47 | + */ |
| 48 | +pub struct Solution {} |
| 49 | + |
| 50 | +// submission codes start here |
| 51 | + |
| 52 | +use std::mem; |
| 53 | +impl Solution { |
| 54 | + pub fn find_min_height_trees(n: i32, edges: Vec<Vec<i32>>) -> Vec<i32> { |
| 55 | + let n = n as usize; |
| 56 | + let mut matrix: Vec<Vec<usize>> = vec![vec![]; n]; |
| 57 | + for edge in edges.iter() { |
| 58 | + matrix[edge[0] as usize].push(edge[1] as usize); |
| 59 | + matrix[edge[1] as usize].push(edge[0] as usize); |
| 60 | + } |
| 61 | + let mut count = n; |
| 62 | + let mut la: Vec<usize> = vec![]; |
| 63 | + let mut lb: Vec<usize> = vec![]; |
| 64 | + for i in 0..n { |
| 65 | + if matrix[i].len() <= 1 { |
| 66 | + la.push(i); |
| 67 | + } |
| 68 | + } |
| 69 | + while count > 2 { |
| 70 | + count -= la.len(); |
| 71 | + for &i in la.iter() { |
| 72 | + let j = matrix[i][0]; |
| 73 | + let idx = matrix[j].iter().position(|&r| r == i).unwrap(); |
| 74 | + matrix[j].remove(idx); |
| 75 | + if matrix[j].len() == 1 { |
| 76 | + lb.push(j); |
| 77 | + } |
| 78 | + } |
| 79 | + la.clear(); |
| 80 | + mem::swap(&mut la, &mut lb); |
| 81 | + } |
| 82 | + la.into_iter().map(|i| i as i32).collect() |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// submission codes end |
| 87 | + |
| 88 | +#[cfg(test)] |
| 89 | +mod tests { |
| 90 | + use super::*; |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn test_310() { |
| 94 | + assert_eq!(Solution::find_min_height_trees(4, vec![vec![1, 0], vec![1, 2], vec![1, 3]]), vec![1]); |
| 95 | + assert_eq!(Solution::find_min_height_trees(6, vec![vec![0, 3], vec![1, 3], vec![2, 3], vec![4, 3], vec![5, 4]]), vec![3, 4]); |
| 96 | + assert_eq!(Solution::find_min_height_trees(1, vec![]), vec![0]); |
| 97 | + } |
| 98 | +} |
0 commit comments