As reported: Cohesity MTS-3, May 2025, round 1 (DSA on CoderPad), second problem with about 20 minutes left: a brute-force idea first, then a single post-order DFS, written as short pseudocode.
This is LeetCode 1110 — Delete Nodes And Return Forest. Our write-up: /delete-nodes-and-return-forest.
Given the root of a binary tree where every node has a distinct value, and a list to_delete, delete every node whose value is in to_delete. The tree splits into a forest (a collection of disjoint trees). Return the roots of the trees in the remaining forest, in any order.
Example 1
root = [1,2,3,4,5,6,7], to_delete = [3,5]
Output: [[1,2,null,4],[6],[7]]
Example 2
root = [1,2,4,null,3], to_delete = [3]
Output: [[1,2,4]]
Constraints: the number of nodes is at most 1000, node values are distinct and between 1 and 1000, to_delete.length <= 1000, to_delete contains distinct values between 1 and 1000.
Code runner format: one JSON value per line: the tree in LeetCode's level-order format, then to_delete. Implement delNodes(root, to_delete) returning the list of roots; the runner prints each remaining tree in level order and accepts the trees in any order.