| | 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.FindElementsInContaminatedBinaryTree; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class FindElementsInContaminatedBinaryTreeHashSet : IFindElementsInContaminatedBinaryTree |
| | 18 | | { |
| 3 | 19 | | private readonly HashSet<int> _hashSet = []; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Time complexity - O(n) |
| | 23 | | /// Space complexity - O(n) |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="root"></param> |
| 3 | 26 | | public FindElementsInContaminatedBinaryTreeHashSet(TreeNode root) |
| 3 | 27 | | { |
| 3 | 28 | | var queue = new Queue<TreeNode>(); |
| | 29 | |
|
| 3 | 30 | | root.val = 0; |
| | 31 | |
|
| 3 | 32 | | queue.Enqueue(root); |
| | 33 | |
|
| 14 | 34 | | while (queue.Count > 0) |
| 11 | 35 | | { |
| 11 | 36 | | var treeNode = queue.Dequeue(); |
| | 37 | |
|
| 11 | 38 | | _hashSet.Add(treeNode.val); |
| | 39 | |
|
| 11 | 40 | | if (treeNode.left != null) |
| 4 | 41 | | { |
| 4 | 42 | | treeNode.left.val = (2 * treeNode.val) + 1; |
| | 43 | |
|
| 4 | 44 | | queue.Enqueue(treeNode.left); |
| 4 | 45 | | } |
| | 46 | |
|
| 11 | 47 | | if (treeNode.right != null) |
| 4 | 48 | | { |
| 4 | 49 | | treeNode.right.val = (2 * treeNode.val) + 2; |
| | 50 | |
|
| 4 | 51 | | queue.Enqueue(treeNode.right); |
| 4 | 52 | | } |
| 11 | 53 | | } |
| 3 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Time complexity - O(1) |
| | 58 | | /// Space complexity - O(1) |
| | 59 | | /// </summary> |
| | 60 | | /// <param name="target"></param> |
| | 61 | | /// <returns></returns> |
| | 62 | | public bool Find(int target) |
| 9 | 63 | | { |
| 9 | 64 | | return _hashSet.Contains(target); |
| 9 | 65 | | } |
| | 66 | | } |