< Summary

Information
Class: LeetCode.Algorithms.NumberOfGoodLeafNodesPairs.NumberOfGoodLeafNodesPairsDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\NumberOfGoodLeafNodesPairs\NumberOfGoodLeafNodesPairsDepthFirstSearch.cs
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 73
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CountPairs(...)100%11100%
CountPairs(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\NumberOfGoodLeafNodesPairs\NumberOfGoodLeafNodesPairsDepthFirstSearch.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.NumberOfGoodLeafNodesPairs;
 15
 16/// <inheritdoc />
 17public class NumberOfGoodLeafNodesPairsDepthFirstSearch : INumberOfGoodLeafNodesPairs
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n * d^2)
 21    ///     Space complexity - O(n * d)
 22    /// </summary>
 23    /// <param name="root"></param>
 24    /// <param name="distance"></param>
 25    /// <returns></returns>
 26    public int CountPairs(TreeNode? root, int distance)
 327    {
 328        var result = 0;
 29
 330        CountPairs(root, distance, ref result);
 31
 332        return result;
 333    }
 34
 35    private static List<int> CountPairs(TreeNode? node, int distance, ref int result)
 2136    {
 2137        if (node == null)
 338        {
 339            return [];
 40        }
 41
 1842        if (node.left == null && node.right == null)
 943        {
 944            return [1];
 45        }
 46
 947        var leftDistances = CountPairs(node.left, distance, ref result);
 948        var rightDistances = CountPairs(node.right, distance, ref result);
 49
 950        result += leftDistances.Sum(leftDistance =>
 2751            rightDistances.Count(rightDistance => leftDistance + rightDistance <= distance));
 52
 953        var currentDistances = new List<int>();
 54
 4355        foreach (var leftDistance in leftDistances)
 856        {
 857            if (leftDistance + 1 <= distance)
 858            {
 859                currentDistances.Add(leftDistance + 1);
 860            }
 861        }
 62
 4763        foreach (var rightDistance in rightDistances)
 1064        {
 1065            if (rightDistance + 1 <= distance)
 966            {
 967                currentDistances.Add(rightDistance + 1);
 968            }
 1069        }
 70
 971        return currentDistances;
 2172    }
 73}