< Summary

Information
Class: LeetCode.Algorithms.BinaryTreePaths.BinaryTreePathsDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\BinaryTreePaths\BinaryTreePathsDepthFirstSearch.cs
Line coverage
100%
Covered lines: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 63
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
BinaryTreePaths(...)100%11100%
BuildTreePaths(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\BinaryTreePaths\BinaryTreePathsDepthFirstSearch.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;
 13using System.Text;
 14
 15namespace LeetCode.Algorithms.BinaryTreePaths;
 16
 17/// <inheritdoc />
 18public class BinaryTreePathsDepthFirstSearch : IBinaryTreePaths
 19{
 20    /// <summary>
 21    ///     Time complexity - O(n)
 22    ///     Space complexity - O(n * h)
 23    /// </summary>
 24    /// <param name="root"></param>
 25    /// <returns></returns>
 26    public IList<string> BinaryTreePaths(TreeNode? root)
 227    {
 228        var treePaths = new List<string>();
 29
 230        BuildTreePaths(root, new StringBuilder(), treePaths);
 31
 232        return treePaths;
 233    }
 34
 35    private static void BuildTreePaths(TreeNode? root, StringBuilder pathStringBuilder, IList<string> treePaths)
 636    {
 637        if (root == null)
 138        {
 139            return;
 40        }
 41
 542        var pathStringBuilderLength = pathStringBuilder.Length;
 43
 544        if (pathStringBuilder.Length > 0)
 345        {
 346            pathStringBuilder.Append("->");
 347        }
 48
 549        pathStringBuilder.Append(root.val);
 50
 551        if (root.left == null && root.right == null)
 352        {
 353            treePaths.Add(pathStringBuilder.ToString());
 354        }
 55        else
 256        {
 257            BuildTreePaths(root.left, pathStringBuilder, treePaths);
 258            BuildTreePaths(root.right, pathStringBuilder, treePaths);
 259        }
 60
 561        pathStringBuilder.Length = pathStringBuilderLength;
 662    }
 63}