< Summary

Information
Class: LeetCode.Algorithms.SmallestStringStartingFromLeaf.SmallestStringStartingFromLeafDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SmallestStringStartingFromLeaf\SmallestStringStartingFromLeafDepthFirstSearch.cs
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 55
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
SmallestFromLeaf(...)100%11100%
FindSmallest(...)100%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SmallestStringStartingFromLeaf\SmallestStringStartingFromLeafDepthFirstSearch.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.SmallestStringStartingFromLeaf;
 15
 16/// <inheritdoc />
 17public class SmallestStringStartingFromLeafDepthFirstSearch : ISmallestStringStartingFromLeaf
 18{
 19    private string? _smallestString;
 20
 21    /// <summary>
 22    ///     Time complexity - O(n) for a balanced tree, O(n^2) for a skewed tree
 23    ///     Space complexity - O(n)
 24    /// </summary>
 25    /// <param name="root"></param>
 26    /// <returns></returns>
 27    public string? SmallestFromLeaf(TreeNode? root)
 328    {
 329        FindSmallest(root, string.Empty);
 30
 331        return _smallestString;
 332    }
 33
 34    private void FindSmallest(TreeNode? node, string currentPath)
 4335    {
 4336        if (node == null)
 2337        {
 2338            return;
 39        }
 40
 2041        var currentChar = (char)('a' + node.val);
 2042        var newPath = currentChar + currentPath;
 43
 2044        if (node.left == null && node.right == null)
 1045        {
 1046            if (_smallestString == null || string.CompareOrdinal(newPath, _smallestString) < 0)
 547            {
 548                _smallestString = newPath;
 549            }
 1050        }
 51
 2052        FindSmallest(node.left, newPath);
 2053        FindSmallest(node.right, newPath);
 4354    }
 55}