Python | DFS | Easy to understand Solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nTraverse Tree nodes one
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTraverse Tree nodes one by one\nif Tree node is root node and if its not part of is_delete list it requires to be deleted.\nOtherwise move forward. Traverse left tree and pass True if is_delete is True or pass False if is_delete is False because if is_delete is True then next node to be traversed is root node. To delete node we should check at the end if is_delete is True then we should set left and right child as None otherwise return node itself.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N)\n\n# Code\n```\n\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n\n    def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]:\n        result = []\n        to_delete_set = set(to_delete)\n\n        def dfs(node, is_root):\n            if not node:\n                return None\n\n            is_deleted = node.val in to_delete_set\n\n            if is_root and not is_deleted:\n                result.append(node)\n\n            node.left = dfs(node.left, is_deleted)\n            node.right = dfs(node.right, is_deleted)\n\n            return None if is_deleted else node\n\n        dfs(root, True)\n        return result\n```
['Python3']