|
| 1 | +<h2><a href="https://leetcode.com/problems/height-of-binary-tree-after-subtree-removal-queries/">2458. Height of Binary Tree After Subtree Removal Queries</a></h2><h3>Hard</h3><hr><div><p>You are given the <code>root</code> of a <strong>binary tree</strong> with <code>n</code> nodes. Each node is assigned a unique value from <code>1</code> to <code>n</code>. You are also given an array <code>queries</code> of size <code>m</code>.</p> |
| 2 | + |
| 3 | +<p>You have to perform <code>m</code> <strong>independent</strong> queries on the tree where in the <code>i<sup>th</sup></code> query you do the following:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li><strong>Remove</strong> the subtree rooted at the node with the value <code>queries[i]</code> from the tree. It is <strong>guaranteed</strong> that <code>queries[i]</code> will <strong>not</strong> be equal to the value of the root.</li> |
| 7 | +</ul> |
| 8 | + |
| 9 | +<p>Return <em>an array </em><code>answer</code><em> of size </em><code>m</code><em> where </em><code>answer[i]</code><em> is the height of the tree after performing the </em><code>i<sup>th</sup></code><em> query</em>.</p> |
| 10 | + |
| 11 | +<p><strong>Note</strong>:</p> |
| 12 | + |
| 13 | +<ul> |
| 14 | + <li>The queries are independent, so the tree returns to its <strong>initial</strong> state after each query.</li> |
| 15 | + <li>The height of a tree is the <strong>number of edges in the longest simple path</strong> from the root to some node in the tree.</li> |
| 16 | +</ul> |
| 17 | + |
| 18 | +<p> </p> |
| 19 | +<p><strong class="example">Example 1:</strong></p> |
| 20 | +<img alt="" src="https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-1.png" style="width: 495px; height: 281px;"> |
| 21 | +<pre><strong>Input:</strong> root = [1,3,4,2,null,6,5,null,null,null,null,null,7], queries = [4] |
| 22 | +<strong>Output:</strong> [2] |
| 23 | +<strong>Explanation:</strong> The diagram above shows the tree after removing the subtree rooted at node with value 4. |
| 24 | +The height of the tree is 2 (The path 1 -> 3 -> 2). |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p><strong class="example">Example 2:</strong></p> |
| 28 | +<img alt="" src="https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-2.png" style="width: 301px; height: 284px;"> |
| 29 | +<pre><strong>Input:</strong> root = [5,8,9,2,1,3,7,4,6], queries = [3,2,4,8] |
| 30 | +<strong>Output:</strong> [3,2,3,2] |
| 31 | +<strong>Explanation:</strong> We have the following queries: |
| 32 | +- Removing the subtree rooted at node with value 3. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 4). |
| 33 | +- Removing the subtree rooted at node with value 2. The height of the tree becomes 2 (The path 5 -> 8 -> 1). |
| 34 | +- Removing the subtree rooted at node with value 4. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 6). |
| 35 | +- Removing the subtree rooted at node with value 8. The height of the tree becomes 2 (The path 5 -> 9 -> 3). |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | +<p><strong>Constraints:</strong></p> |
| 40 | + |
| 41 | +<ul> |
| 42 | + <li>The number of nodes in the tree is <code>n</code>.</li> |
| 43 | + <li><code>2 <= n <= 10<sup>5</sup></code></li> |
| 44 | + <li><code>1 <= Node.val <= n</code></li> |
| 45 | + <li>All the values in the tree are <strong>unique</strong>.</li> |
| 46 | + <li><code>m == queries.length</code></li> |
| 47 | + <li><code>1 <= m <= min(n, 10<sup>4</sup>)</code></li> |
| 48 | + <li><code>1 <= queries[i] <= n</code></li> |
| 49 | + <li><code>queries[i] != root.val</code></li> |
| 50 | +</ul> |
| 51 | +</div> |
0 commit comments