← 返回 snowflake 的题目列表Max Height of a Tree Skipping Deleted Nodes + Minimum Deletions to Keep Height ≤ K
类型:online_judge
You are given a rooted tree (not necessarily binary) with n nodes labeled 1..n described by parent-child edges. You are also given a boolean array deleted[i] indicating whether node i is in a “deleted state”.
When computing height, you must skip deleted nodes:
A deleted node does not contribute to height.
Its subtree still exists; when traversing downward, you may conceptually “pass through” deleted nodes to their children.
The tree height is the maximum length (in number of nodes) of a path from the root to any reachable non-deleted node after skipping deleted nodes. If there is no non-deleted node reachable, height is 0.
Part 1
Return the maximum height of the tree after skipping deleted nodes.
Part 2 (Follow-up)
You may additionally delete more nodes (mark them deleted). After skipping deleted nodes, the resulting tree height must be at most K. Return the minimum number of additional deletions required.
Constraints
1 <= n <= 2e5
1 <= K <= n
Example
Input
4 3
1 2
1 3
3 4
0 1 0 0
Output
3