Java, recursive, beats 100%
We can go recursevly doing DFS. If we pass node and it\'s parent we can use that parent to disjoin the node.  \nBase case for recursion is when the node is null
We can go recursevly doing DFS. If we pass node and it\'s parent we can use that parent to disjoin the node.  \nBase case for recursion is when the node is null - meaning we reached the leaf. We add root node to result at the begining of the new segment, the flag for this is if parent is null.\nI use HashSet to do fast lookups if node needs to be deleted or not.\n\nComplexity is - O(n) time cause we need to iterate over every element, Set is O(1). Space - O(lgn) - callstack for recursion proportional to the hieght of the tree.\n```\n    List<TreeNode> res;\n    Set<Integer> delSet;\n\n    public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {\n        res = new ArrayList();\n        delSet = new HashSet();\n        //put all indexes to delete in to the set\n        for(int delIdx : to_delete)\n            delSet.add(delIdx);\n\n        helper(root, null, true);\n        return res;\n    }\n\n    void helper(TreeNode n, TreeNode parent, boolean isLeft) {\n        //base case - we reached the leaf\n        if (n == null)\n            return;\n        //this is where we need to add the root of the dijoined tree to result\n        if (parent == null && !delSet.contains(n.val)) {\n            res.add(n);\n        }\n        //if we need to delete this node\n        if (delSet.contains(n.val)) {\n            //and we have parent\n            if (parent != null) {\n                //disjoin the tree\n                if (isLeft)\n                    parent.left = null;\n                else\n                    parent.right = null;\n            }\n            //continue recursion\n            helper(n.left, null, true);\n            helper(n.right, null, false);\n        } else {\n            //if we can keep the node - go deeper with recursion, pass node child as current and\n            //current node as parent, do this for left and right children\n            helper(n.left, n, true);\n            helper(n.right, n, false);\n        }\n    }\n```
['Recursion', 'Java']