[Java] Iterative solution w/ explanation.
\n\n\nWe need to unlink children from their parent nodes if the children has a value that is to be deleted. However, with the given TreeNode class, there aren\'
\n\n\nWe need to unlink children from their parent nodes if the children has a value that is to be deleted. However, with the given TreeNode class, there aren\'t pointers to parents.\nThus we must process our tree with the all the logic performed on the children of the current node and not on the current node. \n\nWhat about the root node? It isn\'t the children of any node so we make it the children of a dummy node.\n\nWe\'ll set the value of the dummy node to be `0` so it doesn\'t conflict with actual range of valid values. We will also add `0` to the set of deleted_values so that our add children.node to res logic works out for the root node.\n\n`\n                    if (hashSet.contains(curr.val))\n                        res.add(right);\n`\n\n\nSolution:\n\n    public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {\n        \n        Set<Integer> hashSet = new HashSet<Integer>();\n        for (int num : to_delete)\n            hashSet.add(num);\n        \n        List<TreeNode> res = new LinkedList<TreeNode>();\n        \n        if (root == null)\n            return res;\n        \n        Queue<TreeNode> queue = new LinkedList<TreeNode>();\n        \n        TreeNode dummy = new TreeNode(0);\n        hashSet.add(0);\n        \n        dummy.left = root;\n        queue.offer(dummy);\n        \n        while (!queue.isEmpty()) {\n            TreeNode curr = queue.poll();\n            \n            if (curr.left != null) {\n                \n                TreeNode left = curr.left;\n                queue.offer(left);\n                if (hashSet.contains(left.val) ) {\n                    curr.left = null;\n                }\n                else {\n                    if (hashSet.contains(curr.val))\n                        res.add(left);\n                }\n            }\n            \n            if (curr.right != null) {\n                \n                TreeNode right = curr.right;\n                queue.offer(right);\n                if (hashSet.contains(right.val) ) {\n                    curr.right = null;\n                }\n                else {\n                    if (hashSet.contains(curr.val))\n                        res.add(right);\n                }\n            }\n        }\n        return res;\n    }
[]