< Summary

Information
Class: LeetCode.Algorithms.PathSum2.PathSum2DepthFirstSearchOptimized
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PathSum2\PathSum2DepthFirstSearchOptimized.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 57
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
PathSum(...)100%11100%
PathSum(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PathSum2\PathSum2DepthFirstSearchOptimized.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 PathSum2DepthFirstSearchOptimized : IPathSum2
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(n)
 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        var result = new List<IList<int>>();
 1229        var currentPath = new List<int>();
 30
 1231        PathSum(root, targetSum, currentPath, result);
 32
 1233        return result;
 1234    }
 35
 36    private static void PathSum(TreeNode? root, int targetSum, IList<int> currentPath, ICollection<IList<int>> result)
 12637    {
 12638        if (root == null)
 6039        {
 6040            return;
 41        }
 42
 6643        currentPath.Add(root.val);
 44
 6645        if (root.left == null && root.right == null && targetSum == root.val)
 946        {
 947            result.Add(new List<int>(currentPath));
 948        }
 49        else
 5750        {
 5751            PathSum(root.left, targetSum - root.val, currentPath, result);
 5752            PathSum(root.right, targetSum - root.val, currentPath, result);
 5753        }
 54
 6655        currentPath.RemoveAt(currentPath.Count - 1);
 12656    }
 57}