Prune Minimum Nodes from Tree to Satisfy Height Budget
Dynamic ProgrammingModel answerLast updated
Determine the minimal set of non-root node deletions required to trim a multi-child tree's height down to a target limit under node-promotion rules.
Given a rooted general tree (where each node can have multiple children) and a maximum target height $H$, identify a set containing the minimum number of node deletions required so that the height of the modified tree does not exceed $H$.
When a non-root node is deleted, it is removed from the tree and all of its direct children are promoted to become direct children of its parent (i.e., its subtrees are preserved and moved up one level). The root node (ID 0) cannot be deleted under any circumstances.
Input Specifications
parent: An array of length $M$ representing the tree topology, whereparent[i]is the ID of the parent of nodei. Node0is always the root, soparent[0] = -1.H: A positive integer representing the maximum allowable height. Height is defined as the total number of retained nodes along the longest path from the root to any leaf (e.g., a tree with only the root node has height 1).
Constraints
- $1 \le M \le 1000$
- $1 \le H \le M$
- Node IDs are integers from
0to $M-1$. - Any valid minimal-cardinality set of node IDs to delete may be returned in any order.
Example 1
Input: parent = [-1, 0, 0], H = 1
Output: [1, 2]
Explanation: Node 0 is the root with children 1 and 2. To reach height 1, both child nodes must be removed.
Example 2
Input: parent = [-1, 0, 1, 1], H = 2
Output: [1]
Explanation: Node 0 is parent of 1, and node 1 is parent of 2 and 3. Retaining node 1 gives height 3 (0 -> 1 -> 2). Deleting node 1 promotes 2 and 3 to be direct children of 0, yielding a tree of height 2 (0 -> 2 and 0 -> 3) using 1 deletion. Deleting {2, 3} also achieves height 2 but requires 2 deletions. Thus, [1] is optimal.
Hints
Hint 1
Tree Dynamic Programming: For each node, compute the minimum cost (deletions) to limit the height of its subtree under different allowed height budgets.
Hint 2
Child Promotion Semantics: If you delete a node u, its height contribution to its parent is the height of its subtrees without adding +1 for node u.
Hint 3
State Definition: Define dp(u, h) as the minimum deletions needed in node u's subtree such that the maximum height of the subtree rooted at u is at most h.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Method Explanation
The problem asks for the minimum number of deletions to restrict a tree's height to at most $H$. Deleting a node promotes its children to its parent. This operation means deleting node $u$ simply shifts the subtrees of $u$'s children up by one level relative to $u$'s parent.
We can solve this using Tree Dynamic Programming.
Let $\text{dp}(u, h)$ be the minimum number of deletions in the subtree of node $u$ such that the height of the tree rooted at $u$ is at most $h$ (where height counts retained nodes).
For any node $u$ and a height budget $h \ge 1$:
-
Option 1: Keep node $u$
- Node $u$ consumes $1$ unit of the height budget.
- Its children must each fit within a height budget of $h - 1$.
- Cost: $\sum_{v \in \text{children}(u)} \text{dp}(v, h - 1)$
-
Option 2: Delete node $u$
- Node $u$ adds $1$ to the total deletion count.
- Node $u$ is removed, so its children do not lose height from node $u$'s existence; they are promoted directly under $u$'s parent. Therefore, each child subtree can use the full height budget $h$.
- Cost: $1 + \sum_{v \in \text{children}(u)} \text{dp}(v, h)$
Note: The root node ($u = 0$) cannot be deleted, so for $u = 0$, Option 2 is invalid.
After populating the DP table from leaves up to the root, the minimal cost is $\text{dp}(0, H)$. To return the set of deleted node IDs, we perform a second recursive pass starting at $\text{dp}(0, H)$ to reconstruct which choice (Keep vs. Delete) yielded the optimal value at each node.
Python 3 Implementation
from collections import defaultdict
def min_deletions_to_limit_height(parent: list[int], H: int) -> list[int]:
M = len(parent)
children = defaultdict(list)
for node in range(1, M):
children[parent[node]].append(node)
# memo[u][h] stores (min_deletions, choice) where choice is True if node u is deleted
memo = {}
def solve(u: int, h: int):
if h <= 0:
return float('inf'), False
if (u, h) in memo:
return memo[(u, h)]
# Option 1: Keep u
keep_cost = 0
for v in children[u]:
c, _ = solve(v, h - 1)
keep_cost += c
# Option 2: Delete u (Root u=0 cannot be deleted)
if u != 0:
delete_cost = 1
for v in children[u]:
c, _ = solve(v, h)
delete_cost += c
else:
delete_cost = float('inf')
if keep_cost <= delete_cost:
res = (keep_cost, False)
else:
res = (delete_cost, True)
memo[(u, h)] = res
return res
# Run DP starting from root with height budget H
solve(0, H)
# Solution Reconstruction
deleted_nodes = []
def reconstruct(u: int, h: int):
_, is_deleted = memo[(u, h)]
if is_deleted:
deleted_nodes.append(u)
for v in children[u]:
reconstruct(v, h)
else:
for v in children[u]:
reconstruct(v, h - 1)
reconstruct(0, H)
return deleted_nodes
Complexity Analysis
- Time Complexity: $\mathcal{O}(M \times H)$. There are $M$ nodes and $H$ possible height values. Each state $(u, h)$ processes the children of $u$. Across all nodes for a fixed $h$, iterating through children visits each edge once ($\mathcal{O}(M)$ time). Thus, for $H$ height levels, total time is $\mathcal{O}(M \times H)$.
- Space Complexity: $\mathcal{O}(M \times H)$ to store memoization results and recursive call stacks.