< Summary

Information
Class: LeetCode.Algorithms.DeleteNodesAndReturnForest.DeleteNodesAndReturnForestDepthFirstSearchRecursive
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DeleteNodesAndReturnForest\DeleteNodesAndReturnForestDepthFirstSearchRecursive.cs
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 62
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
DelNodes(...)100%22100%
DelNodes(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DeleteNodesAndReturnForest\DeleteNodesAndReturnForestDepthFirstSearchRecursive.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.DeleteNodesAndReturnForest;
 15
 16/// <inheritdoc />
 17public class DeleteNodesAndReturnForestDepthFirstSearchRecursive : IDeleteNodesAndReturnForest
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n + d)
 21    ///     Space complexity - O(n + d)
 22    /// </summary>
 23    /// <param name="root"></param>
 24    /// <param name="to_delete"></param>
 25    /// <returns></returns>
 26    public IList<TreeNode> DelNodes(TreeNode? root, int[] to_delete)
 827    {
 828        if (root == null)
 129        {
 130            return new List<TreeNode>();
 31        }
 32
 733        var result = new List<TreeNode>();
 34
 735        var toDeleteHashSet = new HashSet<int>(to_delete);
 36
 737        DelNodes(root, true, toDeleteHashSet, result);
 38
 739        return result;
 840    }
 41
 42    private static TreeNode? DelNodes(TreeNode? node, bool isRoot, IReadOnlySet<int> toDeleteHashSet,
 43        ICollection<TreeNode> result)
 10944    {
 10945        if (node == null)
 5846        {
 5847            return null;
 48        }
 49
 5150        var toBeDeleted = toDeleteHashSet.Contains(node.val);
 51
 5152        if (isRoot && !toBeDeleted)
 1553        {
 1554            result.Add(node);
 1555        }
 56
 5157        node.left = DelNodes(node.left, toBeDeleted, toDeleteHashSet, result);
 5158        node.right = DelNodes(node.right, toBeDeleted, toDeleteHashSet, result);
 59
 5160        return toBeDeleted ? null : node;
 10961    }
 62}