Remove Banned Accounts From a Tree and Return the Remaining Forest
Last updated: November 29, 2026
Quick Overview
Given a binary tree of unique account IDs and a set of banned IDs, delete every banned node. Return the roots of the trees that remain.
Meta
November 29, 20260
0
0 solved
Given a binary tree of unique account IDs and a set of banned IDs, delete every banned node. Return the roots of the trees that remain.
An integrity flavored Meta onsite problem that tests post-order thinking: a node's children must be processed before you decide whether it is deleted. The interviewer looks at how you detect new roots without extra passes.
What the Interviewer Expects
- Use a set for O(1) banned lookups
- Process children first, then the node
- Detach deleted children from their parent
- A surviving node becomes a root when its parent is deleted or absent
- Return the forest in any order
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return the forest sorted by root ID?
- What if deletion should also remove the whole subtree?
- How would you do it iteratively?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input is a root of a tree with unique values and a list banned. Remove those nodes; their children, if not banned, become roots of new trees. Return the list of remaining roots....
High-Level Design
Recurse with a flag is_root, true when the node has no surviving parent. At each node, determine deleted = val in banned. If the node is not deleted and is_root, add it to the output. Recurse in...