| | 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.FindCorrespondingNodeOfBinaryTreeInCloneOfThatTree; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class FindCorrespondingNodeOfBinaryTreeInCloneOfThatTreeDepthFirstSearchRecursive : |
| | 18 | | IFindCorrespondingNodeOfBinaryTreeInCloneOfThatTree |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// Time complexity - O(n) |
| | 22 | | /// Space complexity - O(n) for a skewed tree, O(log n) for balanced tree |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="original"></param> |
| | 25 | | /// <param name="cloned"></param> |
| | 26 | | /// <param name="target"></param> |
| | 27 | | /// <returns></returns> |
| | 28 | | public TreeNode? GetTargetCopy(TreeNode? original, TreeNode? cloned, TreeNode? target) |
| 13 | 29 | | { |
| 13 | 30 | | if (original == null || cloned == null || target == null) |
| 5 | 31 | | { |
| 5 | 32 | | return null; |
| | 33 | | } |
| | 34 | |
|
| 8 | 35 | | if (original.Equals(target)) |
| 3 | 36 | | { |
| 3 | 37 | | return cloned; |
| | 38 | | } |
| | 39 | |
|
| 5 | 40 | | var node = GetTargetCopy(original.left, cloned.left, target); |
| | 41 | |
|
| 5 | 42 | | return node ?? GetTargetCopy(original.right, cloned.right, target); |
| 13 | 43 | | } |
| | 44 | | } |