| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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.HeightOfBinaryTreeAfterSubtreeRemovalQueries; |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public sealed class HeightOfBinaryTreeAfterSubtreeRemovalQueriesDepthFirstSearch : |
| | | 18 | | IHeightOfBinaryTreeAfterSubtreeRemovalQueries |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Time complexity - O(n + m), where n is the number of nodes and m is the number of queries |
| | | 22 | | /// Space complexity - O(n), where n is the number of nodes |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="root"></param> |
| | | 25 | | /// <param name="queries"></param> |
| | | 26 | | /// <returns></returns> |
| | | 27 | | public int[] TreeQueries(TreeNode root, int[] queries) |
| | 3 | 28 | | { |
| | 3 | 29 | | var nodesCount = GetNodesCount(root); |
| | | 30 | | |
| | 3 | 31 | | Span<int> heights = stackalloc int[nodesCount + 1]; |
| | | 32 | | |
| | 3 | 33 | | LeftToRight(root, heights); |
| | | 34 | | |
| | 3 | 35 | | RightToLeft(root, heights); |
| | | 36 | | |
| | 3 | 37 | | return GetAnswer(queries, heights); |
| | 3 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Time complexity - O(n), where n is the number of nodes |
| | | 42 | | /// Space complexity - O(n), where n is the number of nodes |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="root"></param> |
| | | 45 | | /// <param name="heights"></param> |
| | | 46 | | private static void LeftToRight(TreeNode root, Span<int> heights) |
| | 3 | 47 | | { |
| | 3 | 48 | | var maxHeight = 0; |
| | | 49 | | |
| | 3 | 50 | | var nodesStack = new Stack<(TreeNode Node, int CurrentHeight)>(); |
| | | 51 | | |
| | 3 | 52 | | nodesStack.Push((root, -1)); |
| | | 53 | | |
| | 24 | 54 | | while (nodesStack.Count > 0) |
| | 21 | 55 | | { |
| | 21 | 56 | | var (node, currentHeight) = nodesStack.Pop(); |
| | | 57 | | |
| | 21 | 58 | | var nodeValue = node.val; |
| | | 59 | | |
| | 21 | 60 | | heights[nodeValue] = Math.Max(Math.Max(maxHeight, currentHeight), heights[nodeValue]); |
| | | 61 | | |
| | 21 | 62 | | var nextHeight = currentHeight + 1; |
| | | 63 | | |
| | 21 | 64 | | maxHeight = Math.Max(maxHeight, nextHeight); |
| | | 65 | | |
| | 21 | 66 | | var rightNode = node.right; |
| | | 67 | | |
| | 21 | 68 | | if (rightNode != null) |
| | 9 | 69 | | { |
| | 9 | 70 | | nodesStack.Push((rightNode, nextHeight)); |
| | 9 | 71 | | } |
| | | 72 | | |
| | 21 | 73 | | var leftNode = node.left; |
| | | 74 | | |
| | 21 | 75 | | if (leftNode != null) |
| | 9 | 76 | | { |
| | 9 | 77 | | nodesStack.Push((leftNode, nextHeight)); |
| | 9 | 78 | | } |
| | 21 | 79 | | } |
| | 3 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Time complexity - O(n), where n is the number of nodes |
| | | 84 | | /// Space complexity - O(n), where n is the number of nodes |
| | | 85 | | /// </summary> |
| | | 86 | | /// <param name="root"></param> |
| | | 87 | | /// <param name="heights"></param> |
| | | 88 | | private static void RightToLeft(TreeNode root, Span<int> heights) |
| | 3 | 89 | | { |
| | 3 | 90 | | var maxHeight = 0; |
| | | 91 | | |
| | 3 | 92 | | var nodesStack = new Stack<(TreeNode Node, int CurrentHeight)>(); |
| | | 93 | | |
| | 3 | 94 | | nodesStack.Push((root, -1)); |
| | | 95 | | |
| | 24 | 96 | | while (nodesStack.Count > 0) |
| | 21 | 97 | | { |
| | 21 | 98 | | var (node, currentHeight) = nodesStack.Pop(); |
| | | 99 | | |
| | 21 | 100 | | var nodeValue = node.val; |
| | | 101 | | |
| | 21 | 102 | | heights[nodeValue] = Math.Max(Math.Max(maxHeight, currentHeight), heights[nodeValue]); |
| | | 103 | | |
| | 21 | 104 | | var nextHeight = currentHeight + 1; |
| | | 105 | | |
| | 21 | 106 | | maxHeight = Math.Max(maxHeight, nextHeight); |
| | | 107 | | |
| | 21 | 108 | | var leftNode = node.left; |
| | | 109 | | |
| | 21 | 110 | | if (leftNode != null) |
| | 9 | 111 | | { |
| | 9 | 112 | | nodesStack.Push((leftNode, nextHeight)); |
| | 9 | 113 | | } |
| | | 114 | | |
| | 21 | 115 | | var rightNode = node.right; |
| | | 116 | | |
| | 21 | 117 | | if (rightNode != null) |
| | 9 | 118 | | { |
| | 9 | 119 | | nodesStack.Push((rightNode, nextHeight)); |
| | 9 | 120 | | } |
| | 21 | 121 | | } |
| | 3 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Time complexity - O(n), where n is the number of nodes |
| | | 126 | | /// Space complexity - O(1) |
| | | 127 | | /// </summary> |
| | | 128 | | /// <param name="root"></param> |
| | | 129 | | /// <returns></returns> |
| | | 130 | | private static int GetNodesCount(TreeNode? root) |
| | 3 | 131 | | { |
| | 3 | 132 | | var nodesCount = 0; |
| | | 133 | | |
| | 3 | 134 | | var current = root; |
| | | 135 | | |
| | 33 | 136 | | while (current != null) |
| | 30 | 137 | | { |
| | 30 | 138 | | if (current.left == null) |
| | 12 | 139 | | { |
| | 12 | 140 | | nodesCount++; |
| | | 141 | | |
| | 12 | 142 | | current = current.right; |
| | 12 | 143 | | } |
| | | 144 | | else |
| | 18 | 145 | | { |
| | 18 | 146 | | var previous = current.left; |
| | | 147 | | |
| | 24 | 148 | | while (previous.right != null && previous.right != current) |
| | 6 | 149 | | { |
| | 6 | 150 | | previous = previous.right; |
| | 6 | 151 | | } |
| | | 152 | | |
| | 18 | 153 | | if (previous.right == null) |
| | 9 | 154 | | { |
| | 9 | 155 | | previous.right = current; |
| | | 156 | | |
| | 9 | 157 | | current = current.left; |
| | 9 | 158 | | } |
| | | 159 | | else |
| | 9 | 160 | | { |
| | 9 | 161 | | previous.right = null; |
| | | 162 | | |
| | 9 | 163 | | nodesCount++; |
| | | 164 | | |
| | 9 | 165 | | current = current.right; |
| | 9 | 166 | | } |
| | 18 | 167 | | } |
| | 30 | 168 | | } |
| | | 169 | | |
| | 3 | 170 | | return nodesCount; |
| | 3 | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Time complexity - O(m), where m is the number of queries |
| | | 175 | | /// Space complexity - O(1) |
| | | 176 | | /// </summary> |
| | | 177 | | /// <param name="queries"></param> |
| | | 178 | | /// <param name="heights"></param> |
| | | 179 | | /// <returns></returns> |
| | | 180 | | private static int[] GetAnswer(int[] queries, ReadOnlySpan<int> heights) |
| | 3 | 181 | | { |
| | 3 | 182 | | var queriesLength = queries.Length; |
| | | 183 | | |
| | 26 | 184 | | for (var i = 0; i < queriesLength; i++) |
| | 10 | 185 | | { |
| | 10 | 186 | | var query = queries[i]; |
| | | 187 | | |
| | 10 | 188 | | queries[i] = heights[query]; |
| | 10 | 189 | | } |
| | | 190 | | |
| | 3 | 191 | | return queries; |
| | 3 | 192 | | } |
| | | 193 | | } |