Surviving Tree Height and Extra Removals to Meet a Limit
Trees & GraphsAuto-graded · 23 test casesLast updated
Compute the height of a tree after removing marked nodes (cutting off their subtrees), then find the fewest extra removals needed to bring the height within a limit.
Surviving Tree Height and Extra Removals to Meet a Limit
You are given a rooted tree with n nodes labeled 0 to n - 1. Node 0 is the root. The tree is described by children, where children[i] lists the labels of the children of node i. Some nodes are marked as removed in the boolean list removed.
Surviving tree
Removing a node deletes the node and every edge attached to it. A node is reachable only if the path from the root to it contains no removed node. Reachable nodes form the surviving tree. A removed node's whole subtree becomes unreachable, even if some of its descendants are not marked as removed. Unreachable nodes are ignored.
The height of the surviving tree is the largest number of nodes on a path that starts at the root and goes downward through reachable nodes. A lone root has height 1. If the root itself is removed, or the tree is empty, the height is 0.
Task
Given children, removed, and an integer limit, return a list of two integers [height, extra]:
heightis the height of the surviving tree as defined above.extrais the minimum number of additional nodes you must remove so that the height of the surviving tree is at mostlimit.- You may choose any nodes to remove, including the root.
- Removing a node cuts off its entire subtree, as described above.
- If the height is already at most
limit,extrais0.
Both values must be computed for the same input and returned in this order.
Constraints
0 <= n <= 10^5, wheren = len(children) = len(removed).- If
n > 0, node0is the root, andchildrendescribes a valid rooted tree: every other node has exactly one parent. removed[i]isTrueif nodeiis removed.0 <= limit <= 10^5.- The tree can be a long chain, so avoid unbounded recursion.
- If
n == 0or the root is removed, return[0, 0].
Examples
Example 1
Input: pruned_tree_height([[1, 2], [3, 4], [], [], []], [False, False, False, False, False], 2)
Output: [3, 1]
Example 2
Input: pruned_tree_height([[1, 2], [3], [4, 5], [], [], []], [False, False, False, False, True, False], 2)
Output: [3, 1]
Example 3
Input: pruned_tree_height([[1], []], [True, False], 1)
Output: [0, 0]
Example 4
Input: pruned_tree_height([[1, 2], [], []], [False, False, False], 1)
Output: [2, 1]
Hints
Hint 1
Traverse from the root and never step into a removed node. Track the number of nodes on the path to each visited node, and use an explicit stack because the tree may be very deep.
Hint 2
Removing a node cuts off its whole subtree, and the root is allowed to be removed. Think about what a single removal at the top can do.
Hint 3
If the surviving height is already at most the limit, nothing more is needed. Otherwise, one removal of the root brings the height to 0. Check whether the answer can ever be larger than 1.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Part 1: height. Run a depth-first search from the root with an explicit stack. Each stack entry stores a node and the number of nodes on the path from the root to it, so the root has count 1. When expanding a node, push only the children that are not removed. A removed node is never pushed, so everything below it is never visited. This matches the rule that nodes cut off from the root do not count. The height is the largest count seen. If the tree is empty or the root is removed, the answer is 0.
Part 2: extra removals. If the height is already at most limit, no extra removal is needed, so extra = 0.
Otherwise the height exceeds limit. Removing the root cuts off the entire tree, leaving height 0, which is at most limit because limit >= 0. So one removal always suffices. At least one is needed, because with no removal the height is too large. Therefore extra = 1.
Why this is correct
- Removing a node cuts off its whole subtree, and any node may be chosen, including the root. The root removal is therefore a single operation that makes every path disappear.
- The answer can never exceed 1 and is 0 exactly when the height is already within the limit. So
extra = 1if and only ifheight > limit. - Using an explicit stack avoids recursion-depth problems on chain-shaped trees.
- Descendants of a removed node are never reached, so they are never counted, even when they are not marked as removed.
Complexity
- Time: O(n), since each reachable node is visited once.
- Space: O(n) for the stack in the worst case.
Reference solution
from typing import List
def pruned_tree_height(children: List[List[int]], removed: List[bool], limit: int) -> List[int]:
if not children or removed[0]:
return [0, 0]
# Iterative DFS that never enters a removed node.
best = 0
stack = [(0, 1)]
while stack:
node, nodes_on_path = stack.pop()
if nodes_on_path > best:
best = nodes_on_path
for child in children[node]:
if not removed[child]:
stack.append((child, nodes_on_path + 1))
height = best
# Removing the root empties the tree (height 0 <= limit), so at most
# one extra removal is ever needed.
extra = 1 if height > limit else 0
return [height, extra]