< Summary

Information
Class: LeetCode.Algorithms.PathSum2.PathSum2DepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PathSum2\PathSum2DepthFirstSearch.cs
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 58
Line coverage: 100%
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
PathSum(...)50%22100%
PathSum(...)100%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PathSum2\PathSum2DepthFirstSearch.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.PathSum2;
 15
 16/// <inheritdoc />
 17public class PathSum2DepthFirstSearch : IPathSum2
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n log n)
 21    ///     Space complexity - O(n * h)
 22    /// </summary>
 23    /// <param name="root"></param>
 24    /// <param name="targetSum"></param>
 25    /// <returns></returns>
 26    public IList<IList<int>> PathSum(TreeNode? root, int targetSum)
 1227    {
 1228        return root == null ? [] : PathSum(new List<int> { root.val }, root, targetSum, root.val);
 1229    }
 30
 31    private static List<IList<int>> PathSum(IList<int> list, TreeNode root, int targetSum, int currentSum)
 6632    {
 6633        var result = new List<IList<int>>();
 34
 6635        if (root.left == null && root.right == null && currentSum == targetSum)
 936        {
 937            result.Add(list);
 938        }
 39        else
 5740        {
 5741            if (root.left != null)
 3242            {
 3243                result.AddRange(PathSum(new List<int>(list) { root.left.val }, root.left, targetSum,
 3244                    currentSum + root.left.val));
 3245            }
 46
 5747            if (root.right == null)
 3548            {
 3549                return result;
 50            }
 51
 2252            result.AddRange(PathSum(new List<int>(list) { root.right.val }, root.right, targetSum,
 2253                currentSum + root.right.val));
 2254        }
 55
 3156        return result;
 6657    }
 58}