| | 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 FindCorrespondingNodeOfBinaryTreeInCloneOfThatTreeDepthFirstSearchStack : |
| | 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) |
| 3 | 29 | | { |
| 3 | 30 | | if (original == null || cloned == null || target == null) |
| 0 | 31 | | { |
| 0 | 32 | | return null; |
| | 33 | | } |
| | 34 | |
|
| 3 | 35 | | var originalStack = new Stack<TreeNode>(); |
| 3 | 36 | | var clonedStack = new Stack<TreeNode>(); |
| | 37 | |
|
| 3 | 38 | | originalStack.Push(original); |
| 3 | 39 | | clonedStack.Push(cloned); |
| | 40 | |
|
| 7 | 41 | | while (originalStack.Count > 0) |
| 7 | 42 | | { |
| 7 | 43 | | var originalNode = originalStack.Pop(); |
| 7 | 44 | | var clonedNode = clonedStack.Pop(); |
| | 45 | |
|
| 7 | 46 | | if (originalNode.Equals(target)) |
| 3 | 47 | | { |
| 3 | 48 | | return clonedNode; |
| | 49 | | } |
| | 50 | |
|
| 4 | 51 | | if (originalNode.left != null) |
| 1 | 52 | | { |
| 1 | 53 | | originalStack.Push(originalNode.left); |
| 1 | 54 | | } |
| | 55 | |
|
| 4 | 56 | | if (originalNode.right != null) |
| 4 | 57 | | { |
| 4 | 58 | | originalStack.Push(originalNode.right); |
| 4 | 59 | | } |
| | 60 | |
|
| 4 | 61 | | if (clonedNode.left != null) |
| 1 | 62 | | { |
| 1 | 63 | | clonedStack.Push(clonedNode.left); |
| 1 | 64 | | } |
| | 65 | |
|
| 4 | 66 | | if (clonedNode.right != null) |
| 4 | 67 | | { |
| 4 | 68 | | clonedStack.Push(clonedNode.right); |
| 4 | 69 | | } |
| 4 | 70 | | } |
| | 71 | |
|
| 0 | 72 | | return null; |
| 3 | 73 | | } |
| | 74 | | } |