| | 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 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) |
| 6 | 28 | | { |
| 6 | 29 | | var treeNodeStack = new Stack<TreeNode>(); |
| | 30 | |
|
| 6 | 31 | | treeNodeStack.Push(root); |
| | 32 | |
|
| 19 | 33 | | while (treeNodeStack.Count > 0) |
| 18 | 34 | | { |
| 18 | 35 | | var currentTreeNode = treeNodeStack.Pop(); |
| | 36 | |
|
| 18 | 37 | | if (currentTreeNode.val == head.val) |
| 10 | 38 | | { |
| 10 | 39 | | if (IsLocalSubPath(head, currentTreeNode)) |
| 5 | 40 | | { |
| 5 | 41 | | return true; |
| | 42 | | } |
| 5 | 43 | | } |
| | 44 | |
|
| 13 | 45 | | if (currentTreeNode.left != null) |
| 6 | 46 | | { |
| 6 | 47 | | treeNodeStack.Push(currentTreeNode.left); |
| 6 | 48 | | } |
| | 49 | |
|
| 13 | 50 | | if (currentTreeNode.right != null) |
| 7 | 51 | | { |
| 7 | 52 | | treeNodeStack.Push(currentTreeNode.right); |
| 7 | 53 | | } |
| 13 | 54 | | } |
| | 55 | |
|
| 1 | 56 | | return false; |
| 6 | 57 | | } |
| | 58 | |
|
| | 59 | | private static bool IsLocalSubPath(ListNode? listNode, TreeNode? treeNode) |
| 53 | 60 | | { |
| 53 | 61 | | if (listNode == null) |
| 5 | 62 | | { |
| 5 | 63 | | return true; |
| | 64 | | } |
| | 65 | |
|
| 48 | 66 | | if (treeNode == null) |
| 16 | 67 | | { |
| 16 | 68 | | return false; |
| | 69 | | } |
| | 70 | |
|
| 32 | 71 | | if (listNode.val == treeNode.val) |
| 26 | 72 | | { |
| 26 | 73 | | return IsLocalSubPath(listNode.next, treeNode.left) || IsLocalSubPath(listNode.next, treeNode.right); |
| | 74 | | } |
| | 75 | |
|
| 6 | 76 | | return false; |
| 53 | 77 | | } |
| | 78 | | } |