| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.MinimumHeightTrees; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MinimumHeightTreesBreadthFirstSearch : IMinimumHeightTrees |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(V * (V + E)), where V is the number of vertices(nodes) and E is the number of edges. |
| | 19 | | /// Space complexity - O(V + E), where V is the number of vertices(nodes) and E is the number of edges. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="n"></param> |
| | 22 | | /// <param name="edges"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public IList<int> FindMinHeightTrees(int n, int[][] edges) |
| 10 | 25 | | { |
| 10 | 26 | | if (n == 1) |
| 1 | 27 | | { |
| 1 | 28 | | return new List<int> { 0 }; |
| | 29 | | } |
| | 30 | |
|
| 9 | 31 | | var nodesDictionary = GetNodesDictionary(edges); |
| | 32 | |
|
| 9 | 33 | | var maxNodeDepths = GetMaxNodeDepth(nodesDictionary); |
| | 34 | |
|
| 59 | 35 | | var minDepth = maxNodeDepths.Select(v => v.maxDepth).Min(); |
| | 36 | |
|
| 74 | 37 | | return maxNodeDepths.Where(v => v.maxDepth.Equals(minDepth)).Select(v => v.node).ToList(); |
| 10 | 38 | | } |
| | 39 | |
|
| | 40 | | private static Dictionary<int, List<int>> GetNodesDictionary(IEnumerable<int[]> edges) |
| 9 | 41 | | { |
| 9 | 42 | | var nodesDictionary = new Dictionary<int, List<int>>(); |
| | 43 | |
|
| 109 | 44 | | foreach (var edge in edges) |
| 41 | 45 | | { |
| 41 | 46 | | var root = edge[0]; |
| 41 | 47 | | var leaf = edge[1]; |
| | 48 | |
|
| 41 | 49 | | if (nodesDictionary.TryGetValue(root, out var rootValue)) |
| 31 | 50 | | { |
| 31 | 51 | | rootValue.Add(leaf); |
| 31 | 52 | | } |
| | 53 | | else |
| 10 | 54 | | { |
| 10 | 55 | | nodesDictionary.Add(root, [leaf]); |
| 10 | 56 | | } |
| | 57 | |
|
| 41 | 58 | | if (nodesDictionary.TryGetValue(leaf, out var leafValue)) |
| 1 | 59 | | { |
| 1 | 60 | | leafValue.Add(root); |
| 1 | 61 | | } |
| | 62 | | else |
| 40 | 63 | | { |
| 40 | 64 | | nodesDictionary.Add(leaf, [root]); |
| 40 | 65 | | } |
| 41 | 66 | | } |
| | 67 | |
|
| 9 | 68 | | return nodesDictionary; |
| 9 | 69 | | } |
| | 70 | |
|
| | 71 | | private static List<(int node, int maxDepth)> GetMaxNodeDepth(Dictionary<int, List<int>> nodesDictionary) |
| 9 | 72 | | { |
| 9 | 73 | | var maxNodeDepths = new List<(int node, int maxDepth)>(); |
| | 74 | |
|
| 127 | 75 | | foreach (var node in nodesDictionary) |
| 50 | 76 | | { |
| 50 | 77 | | var maxNodeDepth = 0; |
| | 78 | |
|
| 50 | 79 | | var nodesQueue = new Queue<(int, int)>(); |
| | 80 | |
|
| 50 | 81 | | var visitedNodes = new HashSet<int> { node.Key }; |
| | 82 | |
|
| 314 | 83 | | foreach (var value in node.Value) |
| 82 | 84 | | { |
| 82 | 85 | | nodesQueue.Enqueue((value, 1)); |
| 82 | 86 | | } |
| | 87 | |
|
| 320 | 88 | | while (nodesQueue.Count > 0) |
| 270 | 89 | | { |
| 270 | 90 | | var (currentNode, currentNodeDepth) = nodesQueue.Dequeue(); |
| | 91 | |
|
| 270 | 92 | | visitedNodes.Add(currentNode); |
| | 93 | |
|
| 1726 | 94 | | foreach (var edgesDictionaryItemCur in nodesDictionary[currentNode]) |
| 458 | 95 | | { |
| 458 | 96 | | if (visitedNodes.Contains(edgesDictionaryItemCur)) |
| 270 | 97 | | { |
| 270 | 98 | | maxNodeDepth = Math.Max(maxNodeDepth, currentNodeDepth); |
| 270 | 99 | | } |
| | 100 | | else |
| 188 | 101 | | { |
| 188 | 102 | | nodesQueue.Enqueue((edgesDictionaryItemCur, currentNodeDepth + 1)); |
| 188 | 103 | | } |
| 458 | 104 | | } |
| 270 | 105 | | } |
| | 106 | |
|
| 50 | 107 | | maxNodeDepths.Add((node.Key, maxNodeDepth)); |
| 50 | 108 | | } |
| | 109 | |
|
| 9 | 110 | | return maxNodeDepths; |
| 9 | 111 | | } |
| | 112 | | } |