Python – Explanation – DFS
Time: O(n) where n is the number of nodes in the tree.\n\n\nclass Solution:\n    def __init__(self):\n        self.forest = []\n        self.to_delete = None\n 
Time: O(n) where n is the number of nodes in the tree.\n\n```\nclass Solution:\n    def __init__(self):\n        self.forest = []\n        self.to_delete = None\n        \n    def postorder_traversal(self, root):\n\t\t# Validate input\n        if not root:\n            return None\n        \n\t\t# Traverse left tree\n        root.left = self.postorder_traversal(root.left)\n\t\t# Traverse right tree\n        root.right = self.postorder_traversal(root.right)\n        \n\t\t# If the current node has to be deleted\n\t\t# This means if the current node is on\n\t\t# the to_delete elements\n        if root.val in self.to_delete:\n\t\t\t# Add the left node to the forest\n\t\t\t# If it exists\n            if root.left:\n                self.forest.append(root.left)\n            \n\t\t\t# Add the right node to the forest\n\t\t\t# If it exists\n            if root.right:\n                self.forest.append(root.right)\n                \n\t\t\t# Convert the node to None\n            return None\n       \n\t    # If the node in not in to_delete\n\t\t# Just return it\n        return root\n        \n    \n    def delNodes(self, root: TreeNode, to_delete: List[int]) -> List[TreeNode]:\n\t\t# Convert the input to a set, this makes the lookup constant -> O(1).\n        self.to_delete = set(to_delete)\n\t\t# Traverse on the tree\n        self.postorder_traversal(root)\n        \n\t\t# Once we have processed all the nodes\n\t\t# If the given root does not have to be removed\n\t\t# We add the node to the forest\n        if root.val not in self.to_delete:\n            self.forest.append(root)\n        \n\t\t# Return the forest\n        return self.forest\n```
[]