< Summary

Information
Class: LeetCode.Algorithms.MinimumNumberOfOperationsToSortBinaryTreeByLevel.MinimumNumberOfOperationsToSortBinaryTreeByLevelBreadthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumNumberOfOperationsToSortBinaryTreeByLevel\MinimumNumberOfOperationsToSortBinaryTreeByLevelBreadthFirstSearch.cs
Line coverage
100%
Covered lines: 51
Uncovered lines: 0
Coverable lines: 51
Total lines: 94
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinimumOperations(...)100%2020100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumNumberOfOperationsToSortBinaryTreeByLevel\MinimumNumberOfOperationsToSortBinaryTreeByLevelBreadthFirstSearch.cs

#LineLine coverage
 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
 12using LeetCode.Core.Models;
 13
 14namespace LeetCode.Algorithms.MinimumNumberOfOperationsToSortBinaryTreeByLevel;
 15
 16/// <inheritdoc />
 17public 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)
 327    {
 328        var result = 0;
 29
 330        var treeNodesQueue = new Queue<TreeNode>();
 31
 332        if (root.left != null)
 333        {
 334            treeNodesQueue.Enqueue(root.left);
 335        }
 36
 337        if (root.right != null)
 338        {
 339            treeNodesQueue.Enqueue(root.right);
 340        }
 41
 1042        while (treeNodesQueue.Count > 0)
 743        {
 744            var values = new int[treeNodesQueue.Count];
 45
 5246            for (var i = 0; i < values.Length; i++)
 1947            {
 1948                var node = treeNodesQueue.Dequeue();
 49
 1950                values[i] = node.val;
 51
 1952                if (node.left != null)
 853                {
 854                    treeNodesQueue.Enqueue(node.left);
 855                }
 56
 1957                if (node.right != null)
 558                {
 559                    treeNodesQueue.Enqueue(node.right);
 560                }
 1961            }
 62
 763            var indexDictionary = values
 1964                .Select((value, index) => new { Value = value, Index = index })
 1965                .OrderBy(x => x.Value)
 1966                .Select((x, sortedIndex) => new { x.Index, SortedIndex = sortedIndex })
 4567                .ToDictionary(x => x.Index, x => x.SortedIndex);
 68
 769            var visited = new bool[values.Length];
 70
 5271            for (var i = 0; i < values.Length; i++)
 1972            {
 1973                if (visited[i] || indexDictionary[i] == i)
 1474                {
 1475                    continue;
 76                }
 77
 578                var cycleLength = 0;
 579                var current = i;
 80
 1681                while (!visited[current])
 1182                {
 1183                    visited[current] = true;
 1184                    current = indexDictionary[current];
 1185                    cycleLength++;
 1186                }
 87
 588                result += cycleLength - 1;
 589            }
 790        }
 91
 392        return result;
 393    }
 94}