[Python] Iterative DFS Solution, beats 98.96%
Solve this problem in postorder traversal iteratively using stack.\n\nRemember that a root is a node which does not have any parent node.\nIn the sample input, 
Solve this problem in postorder traversal iteratively using `stack`.\n\nRemember that a root is **a node which does not have any parent node.**\nIn the sample input, the nodes that do not have parent node are [1, 6, 7]\n\n![image](https://assets.leetcode.com/users/images/e4bd690e-e2a1-45a0-98a8-e3e8929264d0_1612634338.7503695.png)\n\n\n**Implementation:**\n* Cast` to_delete` to be a set instead of list to speed up the code\n* Store `(parent_root, root)` in stack\n* There are two conditions, `root.val` is either in `to_delete` or not\n\n**Time Complexity**: O(n)\n**Space Complexity**: O(n + h) where h is the height of the binary tree\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    def delNodes(self, root: TreeNode, to_delete: List[int]) -> List[TreeNode]:\n        res = []\n        stack = [(None, root)]\n        to_delete = set(to_delete)\n        \n        while stack:\n            parent, current = stack.pop()\n            if not current: continue\n            \n            if current.val in to_delete:\n                stack.append((None, current.left))\n                stack.append((None, current.right))\n            else:\n                if current.left and current.left.val in to_delete:\n                    stack.append((None, current.left.left))\n                    stack.append((None, current.left.right))\n                    current.left = None\n                else:\n                    stack.append((current, current.left))\n                    \n                \n                if current.right and current.right.val in to_delete:\n                    stack.append((None, current.right.left))\n                    stack.append((None, current.right.right))\n                    current.right = None\n                else:\n                    stack.append((current, current.right))\n                    \n                # define the termination condition is when the parent is None and value isn\'t to be deleted\n                if parent is None and current.val not in to_delete:\n                    res.append(current)\n                    \n        return res\n```
['Depth-First Search', 'Iterator', 'Python', 'Python3']