< Summary

Information
Class: LeetCode.Algorithms.DeleteNodesAndReturnForest.DeleteNodesAndReturnForestDepthFirstSearchStack
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DeleteNodesAndReturnForest\DeleteNodesAndReturnForestDepthFirstSearchStack.cs
Line coverage
100%
Covered lines: 51
Uncovered lines: 0
Coverable lines: 51
Total lines: 94
Line coverage: 100%
Branch coverage
100%
Covered branches: 30
Total branches: 30
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%3030100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DeleteNodesAndReturnForest\DeleteNodesAndReturnForestDepthFirstSearchStack.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 DeleteNodesAndReturnForestDepthFirstSearchStack : 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);
 736        var stack = new Stack<TreeNode>();
 37
 738        var dummyNode = new TreeNode { left = root };
 39
 740        stack.Push(dummyNode);
 41
 6542        while (stack.Count > 0)
 5843        {
 5844            var node = stack.Pop();
 45
 5846            if (node.left != null)
 2847            {
 2848                stack.Push(node.left);
 49
 2850                if (toDeleteHashSet.Contains(node.left.val))
 1151                {
 1152                    if (node.left.left != null && !toDeleteHashSet.Contains(node.left.left.val))
 353                    {
 354                        result.Add(node.left.left);
 355                    }
 56
 1157                    if (node.left.right != null && !toDeleteHashSet.Contains(node.left.right.val))
 258                    {
 259                        result.Add(node.left.right);
 260                    }
 61
 1162                    node.left = null;
 1163                }
 2864            }
 65
 5866            if (node.right != null)
 2367            {
 2368                stack.Push(node.right);
 69
 2370                if (toDeleteHashSet.Contains(node.right.val))
 1271                {
 1272                    if (node.right.left != null && !toDeleteHashSet.Contains(node.right.left.val))
 373                    {
 374                        result.Add(node.right.left);
 375                    }
 76
 1277                    if (node.right.right != null && !toDeleteHashSet.Contains(node.right.right.val))
 378                    {
 379                        result.Add(node.right.right);
 380                    }
 81
 1282                    node.right = null;
 1283                }
 2384            }
 5885        }
 86
 787        if (dummyNode.left != null)
 488        {
 489            result.Add(dummyNode.left);
 490        }
 91
 792        return result;
 893    }
 94}