Level: Unknown Level
Round: Full Journey · Type: Coding · Difficulty: 7/10 · Duration: 60 min · Interviewer: Neutral
Topics: Binary Tree, Tree Traversal, Depth First Search (DFS), Recursion
Location: San Francisco Bay Area
Interview date: 2026-01-11
Coding Question: I was given a binary tree and a list of node IDs to delete. After deleting the nodes, the remaining nodes form a forest. I needed to find the maximum depth of any tree in the resulting forest.
I was given a binary tree and a list of node IDs to delete. When a node is deleted, its children move up to replace it and attach to the parent of the deleted node. If deleting several nodes in a row, the descendants keep moving up until they find a node that is not deleted. If no non-deleted parent exists, those descendants become new roots. After applying all deletions, I needed to find the maximum depth of the resulting forest. A single root counts as 1 level. If every node is deleted, I should return 0.
Example 1:
Input: root = [1,2,3,4,5,null,6], toDelete = [2] Output: 3
Explanation: Delete node 2. Its children (4 and 5) move up to the parent level. The longest path remaining in the tree is 1 -> 3 -> 6. This path has 3 levels.
Example 2:
Input: root = [1,2,3,4,null,null,5], toDelete = [1,3] Output: 2
Explanation: Delete nodes 1 and 3. This splits the tree. Node 2 (which still has child 4) becomes a root. Node 5 also becomes a root. The tallest tree has 2 levels.
Input Limits: Number of nodes is between 1 and 10^5. Node.val is between -10^5 and 10^5. All Node.val entries are unique. The length of toDelete is between 0 and the total number of nodes. The IDs in toDelete are valid IDs found in the tree.