<p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>

<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>

<p>Return the roots of the trees in the remaining forest. You may return the result in any order.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/07/01/screen-shot-2019-07-01-at-53836-pm.png" style="width: 237px; height: 150px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7], to_delete = [3,5]
<strong>Output:</strong> [[1,2,null,4],[6],[7]]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root = [1,2,4,null,3], to_delete = [3]
<strong>Output:</strong> [[1,2,4]]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the given tree is at most <code>1000</code>.</li>
	<li>Each node has a distinct value between <code>1</code> and <code>1000</code>.</li>
	<li><code>to_delete.length &lt;= 1000</code></li>
	<li><code>to_delete</code> contains distinct values between <code>1</code> and <code>1000</code>.</li>
</ul>

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]:
        forest = [root]
        
        for to_delete_val in to_delete:
            to_delete_root_in_forest = []
            for idx, root_tree in enumerate(forest):
                delNodeRes = self.delNode(root_tree, to_delete_val)
                
                curr, leftChild, rightChild = delNodeRes['curr'], delNodeRes['leftChild'], delNodeRes['rightChild']
                if curr == None:
                    to_delete_root_in_forest.append(idx)
                if leftChild != None:
                    forest.append(leftChild)
                if rightChild != None:
                    forest.append(rightChild)

            to_delete_root_in_forest.sort(reverse=True)
            for delete_root_idx in to_delete_root_in_forest:
                del forest[delete_root_idx]
        return forest

    def delNode(self, root, to_delete_val):
        # 지금 보는 root가 지우려는 노드이면
        if (root == None) or (root.val == to_delete_val):
            return {
                'curr': None,
                'leftChild': root.left if root != None else None,
                'rightChild': root.right if root != None else None,
            }
        
        leftRes = self.delNode(root.left, to_delete_val)
        rightRes = self.delNode(root.right, to_delete_val)
        root.left = leftRes['curr']
        root.right = rightRes['curr']

        return {
            'curr': root,
            'leftChild': leftRes['leftChild'] or rightRes['leftChild'],
            'rightChild': leftRes['rightChild'] or rightRes['rightChild'],
        }
        
        
16700