Tree Levels After Node Deletions
Examples
Example 1:
Input: root = [1,2,3,4,5,null,6], toDelete = [2]
Output: 3
Explanation:
Example 2:
Input: root = [1,2,3,4,null,null,5], toDelete = [1,3]
Output: 2
Explanation:
Example 1:
Input: root = [1,2,3,4,5,null,6], toDelete = [2]
Output: 3
Explanation:
Example 2:
Input: root = [1,2,3,4,null,null,5], toDelete = [1,3]
Output: 2
Explanation:
You are given the root of a binary tree where each node value is a unique node ID, and an array toDelete containing node IDs to remove.
When a node is deleted, it is removed from the tree and its existing children are promoted upward to the deleted node's parent level.
If multiple deleted nodes appear on a path, keep promoting descendants upward until they connect to the nearest non-deleted ancestor (or become a root if none exists).
All deletions are applied logically, and the final result can be a forest (multiple roots).
Return the number of levels in the final structure, defined as the maximum depth among all remaining trees.
0.Delete node 2, promote nodes 4 and 5 to the parent level. The deepest remaining path is 1 -> 3 -> 6, so levels = 3.
After deleting 1 and 3, the forest roots include node 2 (with child 4) and node 5. Maximum levels = 2.
1 <= Number of nodes <= 10^5-10^5 <= Node.val <= 10^5Node.val values are unique.0 <= toDelete.length <= Number of nodestoDelete contains unique node IDs from the tree.