| | 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 | |
|
| | 12 | | using LeetCode.Core.Models; |
| | 13 | |
|
| | 14 | | namespace LeetCode.Algorithms.LinkedListInBinaryTree; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public 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) |
| 6 | 28 | | { |
| 6 | 29 | | var patternList = new List<int> { head.val }; |
| | 30 | |
|
| 6 | 31 | | var prefixList = new List<int> { 0 }; |
| | 32 | |
|
| 6 | 33 | | var patternIndex = 0; |
| | 34 | |
|
| 6 | 35 | | var currentListNode = head.next; |
| | 36 | |
|
| 18 | 37 | | while (currentListNode != null) |
| 12 | 38 | | { |
| 13 | 39 | | while (patternIndex > 0 && currentListNode.val != patternList[patternIndex]) |
| 1 | 40 | | { |
| 1 | 41 | | patternIndex = prefixList[patternIndex - 1]; |
| 1 | 42 | | } |
| | 43 | |
|
| 12 | 44 | | if (currentListNode.val == patternList[patternIndex]) |
| 1 | 45 | | { |
| 1 | 46 | | patternIndex++; |
| 1 | 47 | | } |
| | 48 | |
|
| 12 | 49 | | patternList.Add(currentListNode.val); |
| 12 | 50 | | prefixList.Add(patternIndex); |
| | 51 | |
|
| 12 | 52 | | currentListNode = currentListNode.next; |
| 12 | 53 | | } |
| | 54 | |
|
| 6 | 55 | | return IsSubPath(root, 0, patternList, prefixList); |
| 6 | 56 | | } |
| | 57 | |
|
| | 58 | | private static bool IsSubPath(TreeNode? treeNode, int patternIndex, List<int> patternList, List<int> prefixList) |
| 58 | 59 | | { |
| 58 | 60 | | if (treeNode == null) |
| 25 | 61 | | { |
| 25 | 62 | | return false; |
| | 63 | | } |
| | 64 | |
|
| 40 | 65 | | while (patternIndex > 0 && treeNode.val != patternList[patternIndex]) |
| 7 | 66 | | { |
| 7 | 67 | | patternIndex = prefixList[patternIndex - 1]; |
| 7 | 68 | | } |
| | 69 | |
|
| 33 | 70 | | if (treeNode.val == patternList[patternIndex]) |
| 28 | 71 | | { |
| 28 | 72 | | patternIndex++; |
| 28 | 73 | | } |
| | 74 | |
|
| 33 | 75 | | if (patternIndex == patternList.Count) |
| 5 | 76 | | { |
| 5 | 77 | | return true; |
| | 78 | | } |
| | 79 | |
|
| 28 | 80 | | return IsSubPath(treeNode.left, patternIndex, patternList, prefixList) || |
| 28 | 81 | | IsSubPath(treeNode.right, patternIndex, patternList, prefixList); |
| 58 | 82 | | } |
| | 83 | | } |