Tree Depth After Splicing Out Removed Nodes
Trees & GraphsAuto-graded · 26 test casesLast updated
Compute the depth of a rooted tree given as a parent array after splicing out marked nodes, so their children attach to the nearest surviving ancestor.
Tree Depth After Splicing Out Nodes
A rooted tree with n nodes labeled 0 to n - 1 is described by a parent array. Some nodes are marked for removal. Removing a node does not discard its subtree. Instead, the node is spliced out, and each of its children is reattached to the removed node's parent. If that parent is also removed, the children keep climbing until they reach the closest ancestor that is not removed.
Return the depth of the tree after all the removals, measured as the number of nodes on the longest root-to-node path.
Details
- Node
0is the root andparent[0] == -1. For every other nodei,parent[i]is its parent. A parent may have a larger index than its child. - The root is never removed.
- The final result does not depend on the order in which the removals are applied.
- Depth counts nodes, not edges. A tree consisting of only the root has depth
1.
Function
Implement tree_depth_after_removals(parent, removed), where parent is the parent array and removed is the list of nodes to splice out. Return an integer.
Constraints
1 <= n == len(parent) <= 10^5parent[0] == -1; fori >= 1,0 <= parent[i] <= n - 1, and the links form a single tree rooted at0with no cycles.0 <= len(removed) <= n - 1- Values in
removedare distinct and lie between1andn - 1. - The tree may be a single path, so its depth can be as large as
n.
Examples
Example 1
Input: tree_depth_after_removals([-1, 0, 0, 1, 1, 3, 2], [1])
Output: 3
Example 2
Input: tree_depth_after_removals([-1, 0, 0, 1, 1, 3, 2], [1, 3, 2])
Output: 2
Example 3
Input: tree_depth_after_removals([-1, 0, 0, 1, 1, 3, 2], [])
Output: 4
Hints
Hint 1
Try not to simulate the reattachment. Ask what a surviving node's depth is in terms of its original ancestors.
Hint 2
A surviving node's new depth equals the number of non-removed nodes on its original root-to-node path, including itself and the root.
Hint 3
Build a children list and traverse from the root iteratively (parents can have larger indices, and the tree can be 10^5 deep). A removed child inherits its parent's depth; a surviving child adds 1.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Splicing nodes out never changes the relative order of the surviving nodes on any root path. A surviving node's ancestors after all removals are therefore exactly its original ancestors that were not removed. Its new depth is the number of surviving nodes on its original root-to-node path, counting the root and itself.
This gives a simple top-down recurrence over the original tree:
depth[0] = 1, since the root is never removed.- For a child
cofx: ifcsurvives,depth[c] = depth[x] + 1; ifcis removed,depth[c] = depth[x].
The answer is the maximum value in depth.
Why it is correct
- Ancestry among survivors is preserved: each removal only deletes one node from the paths that contain it, so two survivors keep their ancestor/descendant relationship.
- Because the count only depends on which nodes are removed and not on when, the result is independent of the order of removals.
- A removed node gets the depth of its nearest surviving ancestor, a value that already appears among survivors, so including removed nodes in the maximum cannot inflate the answer.
Implementation notes
- Parents may have larger indices than children, so build a children list and traverse from the root rather than scanning by index.
- Use an iterative BFS, since a path-shaped tree of depth 10^5 would break recursion.
- A boolean array gives O(1) removal lookups.
Complexity
- Time: O(n + k), where
k = len(removed). - Space: O(n).
Reference solution
from typing import List
def tree_depth_after_removals(parent: List[int], removed: List[int]) -> int:
n = len(parent)
gone = [False] * n
for r in removed:
gone[r] = True
children = [[] for _ in range(n)]
for i in range(1, n):
children[parent[i]].append(i)
depth = [0] * n
depth[0] = 1
best = 1
order = [0]
for x in order:
dx = depth[x]
for c in children[x]:
dc = dx if gone[c] else dx + 1
depth[c] = dc
if dc > best:
best = dc
order.append(c)
return best