< Summary

Information
Class: LeetCode.Algorithms.LinkedListInBinaryTree.LinkedListInBinaryTreeKnuthMorrisPratt
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LinkedListInBinaryTree\LinkedListInBinaryTreeKnuthMorrisPratt.cs
Line coverage
100%
Covered lines: 39
Uncovered lines: 0
Coverable lines: 39
Total lines: 83
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
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%88100%
IsSubPath(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LinkedListInBinaryTree\LinkedListInBinaryTreeKnuthMorrisPratt.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 LinkedListInBinaryTreeKnuthMorrisPratt : 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 patternList = new List<int> { head.val };
 30
 631        var prefixList = new List<int> { 0 };
 32
 633        var patternIndex = 0;
 34
 635        var currentListNode = head.next;
 36
 1837        while (currentListNode != null)
 1238        {
 1339            while (patternIndex > 0 && currentListNode.val != patternList[patternIndex])
 140            {
 141                patternIndex = prefixList[patternIndex - 1];
 142            }
 43
 1244            if (currentListNode.val == patternList[patternIndex])
 145            {
 146                patternIndex++;
 147            }
 48
 1249            patternList.Add(currentListNode.val);
 1250            prefixList.Add(patternIndex);
 51
 1252            currentListNode = currentListNode.next;
 1253        }
 54
 655        return IsSubPath(root, 0, patternList, prefixList);
 656    }
 57
 58    private static bool IsSubPath(TreeNode? treeNode, int patternIndex, List<int> patternList, List<int> prefixList)
 5859    {
 5860        if (treeNode == null)
 2561        {
 2562            return false;
 63        }
 64
 4065        while (patternIndex > 0 && treeNode.val != patternList[patternIndex])
 766        {
 767            patternIndex = prefixList[patternIndex - 1];
 768        }
 69
 3370        if (treeNode.val == patternList[patternIndex])
 2871        {
 2872            patternIndex++;
 2873        }
 74
 3375        if (patternIndex == patternList.Count)
 576        {
 577            return true;
 78        }
 79
 2880        return IsSubPath(treeNode.left, patternIndex, patternList, prefixList) ||
 2881               IsSubPath(treeNode.right, patternIndex, patternList, prefixList);
 5882    }
 83}