< Summary

Information
Class: LeetCode.Algorithms.SumRootToLeafNumbers.SumRootToLeafNumbersDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SumRootToLeafNumbers\SumRootToLeafNumbersDepthFirstSearch.cs
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 53
Line coverage: 100%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SumNumbers(...)50%22100%
GetSum(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SumRootToLeafNumbers\SumRootToLeafNumbersDepthFirstSearch.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.SumRootToLeafNumbers;
 15
 16/// <inheritdoc />
 17public class SumRootToLeafNumbersDepthFirstSearch : ISumRootToLeafNumbers
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(n) for a skewed tree, O(log n) for a balanced tree
 22    /// </summary>
 23    /// <param name="root"></param>
 24    /// <returns></returns>
 25    public int SumNumbers(TreeNode? root)
 426    {
 427        return root == null ? 0 : GetSum(root, 0);
 428    }
 29
 30    private static int GetSum(TreeNode treeNode, int currentSum)
 1131    {
 1132        currentSum = (currentSum * 10) + treeNode.val;
 33
 1134        if (treeNode.left == null && treeNode.right == null)
 735        {
 736            return currentSum;
 37        }
 38
 439        var sum = 0;
 40
 441        if (treeNode.left != null)
 442        {
 443            sum += GetSum(treeNode.left, currentSum);
 444        }
 45
 446        if (treeNode.right != null)
 347        {
 348            sum += GetSum(treeNode.right, currentSum);
 349        }
 50
 451        return sum;
 1152    }
 53}