Compute height of tree with deleted nodes; minimize deletions
Company: Snowflake
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of tree data structures, connectivity after node deletions, and optimization for minimizing additional removals, and is categorized under Coding & Algorithms. It is commonly asked to assess reasoning about tree pruning and height computation under constraints, testing both conceptual understanding of tree properties and practical algorithmic implementation.
Part 1: Height of the Effective Tree
Constraints
- 0 <= n <= 200000
- If n > 0, node 0 is the root
- children describes a valid rooted tree with no cycles
- When n > 0, the total number of child references is n - 1
- deleted has length n
Examples
Input: (5, [[1, 2], [3, 4], [], [], []], [False, False, False, False, False])
Expected Output:
Explanation: All nodes are present. The longest root-to-leaf path is 0 -> 1 -> 3 or 0 -> 1 -> 4, so the height is 3.
Input: (6, [[1, 2], [3], [4, 5], [], [], []], [False, True, False, False, False, False])
Expected Output:
Explanation: Node 1 is deleted, so node 3 becomes disconnected and is ignored. The longest remaining path is 0 -> 2 -> 4 or 0 -> 2 -> 5.
Input: (3, [[1, 2], [], []], [True, False, False])
Expected Output:
Explanation: The root is deleted, so the effective tree is empty.
Input: (0, [], [])
Expected Output:
Explanation: An empty tree has height 0.
Hints
- Start a DFS or BFS from the root. Never move into a deleted node.
- Track depth as the number of nodes on the current path, not the number of edges.
Part 2: Minimum Additional Deletions to Limit Height
Constraints
- 0 <= n <= 2000
- If n > 0, node 0 is the root
- children describes a valid rooted tree with no cycles
- When n > 0, the total number of child references is n - 1
- deleted has length n
- 1 <= K <= 2000
- n * K <= 2000000
- Only non-root nodes may be additionally deleted
Examples
Input: (6, [[1, 2], [3, 4], [5], [], [], []], [False, False, False, False, False, False], 2)
Expected Output:
Explanation: To keep height at most 2, the subtree under node 1 must be cut and the subtree under node 2 must also be cut. Deleting nodes 1 and 2 is optimal.
Input: (4, [[1], [2], [3], []], [False, False, False, False], 2)
Expected Output:
Explanation: Deleting node 2 makes the remaining effective tree 0 -> 1, whose height is 2.
Input: (5, [[1], [2], [3], [4], []], [False, False, True, False, False], 2)
Expected Output:
Explanation: Node 2 is already deleted, so nodes 3 and 4 are disconnected. The effective tree is just 0 -> 1 and already has height 2.
Input: (3, [[1, 2], [], []], [True, False, False], 1)
Expected Output:
Explanation: The root is already deleted, so the effective tree is empty.
Input: (1, [[]], [False], 1)
Expected Output:
Explanation: A single-node tree already has height 1, so no additional deletions are needed.
Hints
- Think bottom-up. If you keep a node and allow height h from that node downward, each child must either be deleted or be reduced to height h - 1.
- Define a DP state for keeping node u with remaining allowed height h.