< Summary

Information
Class: LeetCode.Algorithms.RangeSumOfBST.RangeSumOfBSTDepthFirstSearchStack
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RangeSumOfBST\RangeSumOfBSTDepthFirstSearchStack.cs
Line coverage
92%
Covered lines: 23
Uncovered lines: 2
Coverable lines: 25
Total lines: 62
Line coverage: 92%
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
RangeSumBST(...)91.66%121292%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RangeSumOfBST\RangeSumOfBSTDepthFirstSearchStack.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.RangeSumOfBST;
 15
 16/// <inheritdoc />
 17public class RangeSumOfBSTDepthFirstSearchStack : IRangeSumOfBST
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(h), where h is the height of the tree
 22    /// </summary>
 23    /// <param name="root"></param>
 24    /// <param name="low"></param>
 25    /// <param name="high"></param>
 26    /// <returns></returns>
 27    public int RangeSumBST(TreeNode? root, int low, int high)
 228    {
 229        if (root == null)
 030        {
 031            return 0;
 32        }
 33
 234        var result = 0;
 35
 236        var stack = new Stack<TreeNode>();
 37
 238        stack.Push(root);
 39
 1740        while (stack.Count > 0)
 1541        {
 1542            var treeNode = stack.Pop();
 43
 1544            if (treeNode.val >= low && treeNode.val <= high)
 645            {
 646                result += treeNode.val;
 647            }
 48
 1549            if (treeNode.left != null)
 750            {
 751                stack.Push(treeNode.left);
 752            }
 53
 1554            if (treeNode.right != null)
 655            {
 656                stack.Push(treeNode.right);
 657            }
 1558        }
 59
 260        return result;
 261    }
 62}