Skip to content

Practice Height of Binary Tree After Subtree Removal Queries

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2458
LeetCode Problem

Height of Binary Tree After Subtree Removal Queries

Problem Statement

For each queries value, independently remove the entire subtree rooted at the node with that value from root. Node values are unique and no query removes the root. Return the remaining tree height for each query, measured in edges from root to the deepest node. Restore the original tree before each next query.

Source: Height of Binary Tree After Subtree Removal Queries on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
root = [1,3,4,2,null,6,5,null,null,null,null,null,7], queries = [4]
Output
[2]

# Example 2

Input
root = [5,8,9,2,1,3,7,4,6], queries = [3,2,4,8]
Output
[3,2,3,2]

Constraints

The number of nodes in the tree is n.
2 <= n <= 10^5
1 <= Node.val <= n
All the values in the tree are unique.
m == queries.length
1 <= m <= min(n, 10^4)
1 <= queries[i] <= n
queries[i] != root.val

Related LeetCode Problems

Practice more problems similar to Height of Binary Tree After Subtree Removal Queries.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.