< Summary

Information
Class: LeetCode.Algorithms.KthLargestSumInBinaryTree.KthLargestSumInBinaryTreeBreadthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\KthLargestSumInBinaryTree\KthLargestSumInBinaryTreeBreadthFirstSearch.cs
Line coverage
93%
Covered lines: 30
Uncovered lines: 2
Coverable lines: 32
Total lines: 72
Line coverage: 93.7%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
KthLargestLevelSum(...)91.66%121293.75%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\KthLargestSumInBinaryTree\KthLargestSumInBinaryTreeBreadthFirstSearch.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.KthLargestSumInBinaryTree;
 15
 16/// <inheritdoc />
 17public class KthLargestSumInBinaryTreeBreadthFirstSearch : IKthLargestSumInBinaryTree
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n log k)
 21    ///     Space complexity - O(n)
 22    /// </summary>
 23    /// <param name="root"></param>
 24    /// <param name="k"></param>
 25    /// <returns></returns>
 26    public long KthLargestLevelSum(TreeNode root, int k)
 227    {
 228        var nodesQueue = new Queue<TreeNode>();
 29
 230        nodesQueue.Enqueue(root);
 31
 232        var sumsPriorityQueue = new PriorityQueue<long, long>();
 33
 934        while (nodesQueue.Count > 0)
 735        {
 736            var levelCount = nodesQueue.Count;
 37
 738            long sum = 0;
 39
 3840            for (var j = 0; j < levelCount; j++)
 1241            {
 1242                var node = nodesQueue.Dequeue();
 43
 1244                sum += node.val;
 45
 1246                if (node.left != null)
 647                {
 648                    nodesQueue.Enqueue(node.left);
 649                }
 50
 1251                if (node.right != null)
 452                {
 453                    nodesQueue.Enqueue(node.right);
 454                }
 1255            }
 56
 757            sumsPriorityQueue.Enqueue(sum, sum);
 58
 759            if (sumsPriorityQueue.Count > k)
 460            {
 461                sumsPriorityQueue.Dequeue();
 462            }
 763        }
 64
 265        if (sumsPriorityQueue.Count < k)
 066        {
 067            return -1;
 68        }
 69
 270        return sumsPriorityQueue.Dequeue();
 271    }
 72}