< Summary

Information
Class: LeetCode.Algorithms.LinkedListInBinaryTree.LinkedListInBinaryTreeDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LinkedListInBinaryTree\LinkedListInBinaryTreeDepthFirstSearch.cs
Line coverage
100%
Covered lines: 35
Uncovered lines: 0
Coverable lines: 35
Total lines: 78
Line coverage: 100%
Branch coverage
100%
Covered branches: 18
Total branches: 18
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsSubPath(...)100%1010100%
IsLocalSubPath(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LinkedListInBinaryTree\LinkedListInBinaryTreeDepthFirstSearch.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.LinkedListInBinaryTree;
 15
 16/// <inheritdoc />
 17public class LinkedListInBinaryTreeDepthFirstSearch : ILinkedListInBinaryTree
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n * l), where n is the length of the tree and l is the length of the linked list
 21    ///     Space complexity - O(n + l) for an unbalanced tree, O(log n + l) for a balanced tree, where n is the length 
 22    ///     tree and l is the length of the linked list
 23    /// </summary>
 24    /// <param name="head"></param>
 25    /// <param name="root"></param>
 26    /// <returns></returns>
 27    public bool IsSubPath(ListNode head, TreeNode root)
 628    {
 629        var treeNodeStack = new Stack<TreeNode>();
 30
 631        treeNodeStack.Push(root);
 32
 1933        while (treeNodeStack.Count > 0)
 1834        {
 1835            var currentTreeNode = treeNodeStack.Pop();
 36
 1837            if (currentTreeNode.val == head.val)
 1038            {
 1039                if (IsLocalSubPath(head, currentTreeNode))
 540                {
 541                    return true;
 42                }
 543            }
 44
 1345            if (currentTreeNode.left != null)
 646            {
 647                treeNodeStack.Push(currentTreeNode.left);
 648            }
 49
 1350            if (currentTreeNode.right != null)
 751            {
 752                treeNodeStack.Push(currentTreeNode.right);
 753            }
 1354        }
 55
 156        return false;
 657    }
 58
 59    private static bool IsLocalSubPath(ListNode? listNode, TreeNode? treeNode)
 5360    {
 5361        if (listNode == null)
 562        {
 563            return true;
 64        }
 65
 4866        if (treeNode == null)
 1667        {
 1668            return false;
 69        }
 70
 3271        if (listNode.val == treeNode.val)
 2672        {
 2673            return IsLocalSubPath(listNode.next, treeNode.left) || IsLocalSubPath(listNode.next, treeNode.right);
 74        }
 75
 676        return false;
 5377    }
 78}