KatInterview

Surviving Tree Height and Extra Removals to Meet a Limit

Trees & GraphsAuto-graded · 23 test cases
MediumAuto-gradedTrees & Graphstreedfsiterative-traversal

Last 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]:

  1. height is the height of the surviving tree as defined above.
  2. extra is the minimum number of additional nodes you must remove so that the height of the surviving tree is at most limit.
    • 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, extra is 0.

Both values must be computed for the same input and returned in this order.

Constraints

  • 0 <= n <= 10^5, where n = len(children) = len(removed).
  • If n > 0, node 0 is the root, and children describes a valid rooted tree: every other node has exactly one parent.
  • removed[i] is True if node i is removed.
  • 0 <= limit <= 10^5.
  • The tree can be a long chain, so avoid unbounded recursion.
  • If n == 0 or 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.