<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.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int find( int i, const vector< int >& parent ) {
        if( parent[ i ] == -1 ) {
            return -1;
        }
        while( i != parent[ i ] ){
            i = parent[ i ];
        }
        return i;
    }

    void levelOrder( TreeNode* root,
                     unordered_map< int, pair< int, TreeNode* >>& hashMap,
                     const vector< int >& to_delete,
                     vector<int>& parent ) {
        if( root == NULL ) {
            return;
        }

        unordered_set< int > nodesToDelete( to_delete.begin(), to_delete.end() );
        int count = 0;
        hashMap[ root->val ] = { count, root };

        queue< pair< TreeNode*, TreeNode* >> q;
        q.push( { root, NULL } );

        while( !q.empty() ) {
            auto front = q.front();
            q.pop();
            TreeNode* node = front.first;
            TreeNode* parentNode = front.second;

            if( parentNode == NULL ) {
                parent.push_back( hashMap[ node->val ].first );
            } else {
                parent.push_back( hashMap[ parentNode->val ].first );
            }

            if( node->left ) {
                count++;
                hashMap[ node->left->val ] = { count, node->left };
                if( nodesToDelete.find( node->left->val ) != nodesToDelete.end() ) {
                    q.push( { node->left, NULL } );
                    node->left = NULL;
                } else {
                    q.push( { node->left, node } );
                }
            }

            if( node->right ) {
                count++;
                hashMap[ node->right->val ] = { count, node->right };
                if( nodesToDelete.find( node->right->val ) != nodesToDelete.end() ) {
                    q.push( { node->right, NULL } );
                    node->right = NULL;
                } else {
                    q.push( { node->right, node } );
                }
            }
        }
    }

    vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {
        if( root == NULL ) {
            return vector<TreeNode*>();
        }

        // Level order traversal to create the parent array of the nodes in the tree.
        unordered_map< int, pair< int, TreeNode* > > hashMap;
        vector< int > parent;
        levelOrder( root, hashMap, to_delete, parent );

        // Now delete the connections;
        for( const auto& num : to_delete ) {
            int nodeNumInDsu = hashMap[ num ].first;
            parent[ nodeNumInDsu ] = -1;
            for( int i = 0; i < parent.size(); i++ ) {
                if( parent[ i ] == nodeNumInDsu ) {
                    parent[ i ] = i;
                }
            }
        }

        unordered_set< int > distinctRootsInDsu;
        for( int i = 0; i < parent.size(); i++ ) {
            distinctRootsInDsu.insert( find( i, parent ) );
        }

        vector< TreeNode* > result;
        for( const auto& [ _, nodeNumAndNode ] : hashMap ) {
            if( distinctRootsInDsu.find( nodeNumAndNode.first ) != distinctRootsInDsu.end() ) {
                result.push_back( nodeNumAndNode.second );
            }
        }

        return result;
    }
};
29788