< Summary

Information
Class: LeetCode.Algorithms.RecoverTreeFromPreorderTraversal.RecoverTreeFromPreorderTraversalDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RecoverTreeFromPreorderTraversal\RecoverTreeFromPreorderTraversalDepthFirstSearch.cs
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 82
Line coverage: 100%
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
RecoverFromPreorder(...)100%11100%
Build(...)100%44100%
GetValue(...)100%44100%
GetDepth(...)75%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RecoverTreeFromPreorderTraversal\RecoverTreeFromPreorderTraversalDepthFirstSearch.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.RecoverTreeFromPreorderTraversal;
 15
 16/// <inheritdoc />
 17public class RecoverTreeFromPreorderTraversalDepthFirstSearch : IRecoverTreeFromPreorderTraversal
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(n)
 22    /// </summary>
 23    /// <param name="traversal"></param>
 24    /// <returns></returns>
 25    public TreeNode RecoverFromPreorder(string traversal)
 326    {
 327        var index = 0;
 28
 329        return Build(traversal, 0, ref index)!;
 330    }
 31
 32    private static TreeNode? Build(string traversal, int currentDepth, ref int index)
 4133    {
 4134        if (index >= traversal.Length)
 935        {
 936            return null;
 37        }
 38
 3239        var depth = GetDepth(traversal, ref index);
 40
 3241        if (depth != currentDepth)
 1342        {
 1343            index -= depth;
 44
 1345            return null;
 46        }
 47
 1948        var value = GetValue(traversal, ref index);
 1949        var left = Build(traversal, currentDepth + 1, ref index);
 1950        var right = Build(traversal, currentDepth + 1, ref index);
 51
 1952        return new TreeNode(value, left, right);
 4153    }
 54
 55    private static int GetValue(string traversal, ref int index)
 1956    {
 1957        var value = 0;
 58
 4459        while (index < traversal.Length && char.IsDigit(traversal[index]))
 2560        {
 2561            value *= 10;
 2562            value += traversal[index] - '0';
 63
 2564            index++;
 2565        }
 66
 1967        return value;
 1968    }
 69
 70    private static int GetDepth(string traversal, ref int index)
 3271    {
 3272        var depth = 0;
 73
 8274        while (index < traversal.Length && traversal[index] == '-')
 5075        {
 5076            depth++;
 5077            index++;
 5078        }
 79
 3280        return depth;
 3281    }
 82}