| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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 | | using LeetCode.Core.Models; |
| | | 13 | | |
| | | 14 | | namespace LeetCode.Algorithms.MinimumNumberOfOperationsToSortBinaryTreeByLevel; |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public sealed class MinimumNumberOfOperationsToSortBinaryTreeByLevelBreadthFirstSearch : |
| | | 18 | | IMinimumNumberOfOperationsToSortBinaryTreeByLevel |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Time complexity - O(n log n) |
| | | 22 | | /// Space complexity - O(n) |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="root"></param> |
| | | 25 | | /// <returns></returns> |
| | | 26 | | public int MinimumOperations(TreeNode root) |
| | 3 | 27 | | { |
| | 3 | 28 | | var result = 0; |
| | | 29 | | |
| | 3 | 30 | | var treeNodesQueue = new Queue<TreeNode>(); |
| | | 31 | | |
| | 3 | 32 | | if (root.left != null) |
| | 3 | 33 | | { |
| | 3 | 34 | | treeNodesQueue.Enqueue(root.left); |
| | 3 | 35 | | } |
| | | 36 | | |
| | 3 | 37 | | if (root.right != null) |
| | 3 | 38 | | { |
| | 3 | 39 | | treeNodesQueue.Enqueue(root.right); |
| | 3 | 40 | | } |
| | | 41 | | |
| | 10 | 42 | | while (treeNodesQueue.Count > 0) |
| | 7 | 43 | | { |
| | 7 | 44 | | var values = new int[treeNodesQueue.Count]; |
| | | 45 | | |
| | 52 | 46 | | for (var i = 0; i < values.Length; i++) |
| | 19 | 47 | | { |
| | 19 | 48 | | var node = treeNodesQueue.Dequeue(); |
| | | 49 | | |
| | 19 | 50 | | values[i] = node.val; |
| | | 51 | | |
| | 19 | 52 | | if (node.left != null) |
| | 8 | 53 | | { |
| | 8 | 54 | | treeNodesQueue.Enqueue(node.left); |
| | 8 | 55 | | } |
| | | 56 | | |
| | 19 | 57 | | if (node.right != null) |
| | 5 | 58 | | { |
| | 5 | 59 | | treeNodesQueue.Enqueue(node.right); |
| | 5 | 60 | | } |
| | 19 | 61 | | } |
| | | 62 | | |
| | 7 | 63 | | var indexDictionary = values |
| | 19 | 64 | | .Select((value, index) => new { Value = value, Index = index }) |
| | 19 | 65 | | .OrderBy(x => x.Value) |
| | 19 | 66 | | .Select((x, sortedIndex) => new { x.Index, SortedIndex = sortedIndex }) |
| | 45 | 67 | | .ToDictionary(x => x.Index, x => x.SortedIndex); |
| | | 68 | | |
| | 7 | 69 | | var visited = new bool[values.Length]; |
| | | 70 | | |
| | 52 | 71 | | for (var i = 0; i < values.Length; i++) |
| | 19 | 72 | | { |
| | 19 | 73 | | if (visited[i] || indexDictionary[i] == i) |
| | 14 | 74 | | { |
| | 14 | 75 | | continue; |
| | | 76 | | } |
| | | 77 | | |
| | 5 | 78 | | var cycleLength = 0; |
| | 5 | 79 | | var current = i; |
| | | 80 | | |
| | 16 | 81 | | while (!visited[current]) |
| | 11 | 82 | | { |
| | 11 | 83 | | visited[current] = true; |
| | 11 | 84 | | current = indexDictionary[current]; |
| | 11 | 85 | | cycleLength++; |
| | 11 | 86 | | } |
| | | 87 | | |
| | 5 | 88 | | result += cycleLength - 1; |
| | 5 | 89 | | } |
| | 7 | 90 | | } |
| | | 91 | | |
| | 3 | 92 | | return result; |
| | 3 | 93 | | } |
| | | 94 | | } |