simple C++ solution: beats 100% on memory and time
class Solution {\nprivate:\n    bool helper(vector<TreeNode*>& res, TreeNode* & root, unordered_set<int>& set)\n    {\n        \n        if(root == nullptr)\n  
<pre style=\'color:#000000;background:#ffffff;\'><span style=\'color:#800000; font-weight:bold; \'>class</span> Solution <span style=\'color:#800080; \'>{</span>\n<span style=\'color:#800000; font-weight:bold; \'>private</span><span style=\'color:#e34adc; \'>:</span>\n    <span style=\'color:#800000; font-weight:bold; \'>bool</span> helper<span style=\'color:#808030; \'>(</span><span style=\'color:#603000; \'>vector</span><span style=\'color:#800080; \'>&lt;</span>TreeNode<span style=\'color:#808030; \'>*</span><span style=\'color:#800080; \'>></span><span style=\'color:#808030; \'>&amp;</span> res<span style=\'color:#808030; \'>,</span> TreeNode<span style=\'color:#808030; \'>*</span> <span style=\'color:#808030; \'>&amp;</span> root<span style=\'color:#808030; \'>,</span> unordered_set<span style=\'color:#800080; \'>&lt;</span><span style=\'color:#800000; font-weight:bold; \'>int</span><span style=\'color:#800080; \'>></span><span style=\'color:#808030; \'>&amp;</span> <span style=\'color:#603000; \'>set</span><span style=\'color:#808030; \'>)</span>\n    <span style=\'color:#800080; \'>{</span>\n        \n        <span style=\'color:#800000; font-weight:bold; \'>if</span><span style=\'color:#808030; \'>(</span>root <span style=\'color:#808030; \'>=</span><span style=\'color:#808030; \'>=</span> <span style=\'color:#800000; font-weight:bold; \'>nullptr</span><span style=\'color:#808030; \'>)</span>\n            <span style=\'color:#800000; font-weight:bold; \'>return</span> <span style=\'color:#800000; font-weight:bold; \'>false</span><span style=\'color:#800080; \'>;</span>\n        \n        <span style=\'color:#800000; font-weight:bold; \'>bool</span> del <span style=\'color:#808030; \'>=</span> <span style=\'color:#603000; \'>set</span><span style=\'color:#808030; \'>.</span><span style=\'color:#603000; \'>find</span><span style=\'color:#808030; \'>(</span>root<span style=\'color:#808030; \'>-</span><span style=\'color:#808030; \'>></span>val<span style=\'color:#808030; \'>)</span> <span style=\'color:#808030; \'>!</span><span style=\'color:#808030; \'>=</span> <span style=\'color:#603000; \'>set</span><span style=\'color:#808030; \'>.</span>end<span style=\'color:#808030; \'>(</span><span style=\'color:#808030; \'>)</span><span style=\'color:#800080; \'>;</span>\n        <span style=\'color:#800000; font-weight:bold; \'>bool</span> l <span style=\'color:#808030; \'>=</span> helper<span style=\'color:#808030; \'>(</span>res<span style=\'color:#808030; \'>,</span> root<span style=\'color:#808030; \'>-</span><span style=\'color:#808030; \'>></span>left<span style=\'color:#808030; \'>,</span> <span style=\'color:#603000; \'>set</span><span style=\'color:#808030; \'>)</span><span style=\'color:#800080; \'>;</span>\n        <span style=\'color:#800000; font-weight:bold; \'>bool</span> r <span style=\'color:#808030; \'>=</span> helper<span style=\'color:#808030; \'>(</span>res<span style=\'color:#808030; \'>,</span> root<span style=\'color:#808030; \'>-</span><span style=\'color:#808030; \'>></span>right<span style=\'color:#808030; \'>,</span> <span style=\'color:#603000; \'>set</span><span style=\'color:#808030; \'>)</span><span style=\'color:#800080; \'>;</span>\n        \n        <span style=\'color:#800000; font-weight:bold; \'>if</span><span style=\'color:#808030; \'>(</span>del<span style=\'color:#808030; \'>)</span>\n        <span style=\'color:#800080; \'>{</span>\n           \n            <span style=\'color:#800000; font-weight:bold; \'>if</span><span style=\'color:#808030; \'>(</span><span style=\'color:#808030; \'>!</span>l <span style=\'color:#808030; \'>&amp;</span><span style=\'color:#808030; \'>&amp;</span> root<span style=\'color:#808030; \'>-</span><span style=\'color:#808030; \'>></span>left<span style=\'color:#808030; \'>)</span>\n                res<span style=\'color:#808030; \'>.</span>push_back<span style=\'color:#808030; \'>(</span>root<span style=\'color:#808030; \'>-</span><span style=\'color:#808030; \'>></span>left<span style=\'color:#808030; \'>)</span><span style=\'color:#800080; \'>;</span>\n            <span style=\'color:#800000; font-weight:bold; \'>if</span><span style=\'color:#808030; \'>(</span><span style=\'color:#808030; \'>!</span>r <span style=\'color:#808030; \'>&amp;</span><span style=\'color:#808030; \'>&amp;</span> root<span style=\'color:#808030; \'>-</span><span style=\'color:#808030; \'>></span>right<span style=\'color:#808030; \'>)</span>\n                res<span style=\'color:#808030; \'>.</span>push_back<span style=\'color:#808030; \'>(</span>root<span style=\'color:#808030; \'>-</span><span style=\'color:#808030; \'>></span>right<span style=\'color:#808030; \'>)</span><span style=\'color:#800080; \'>;</span>\n            root <span style=\'color:#808030; \'>=</span> <span style=\'color:#800000; font-weight:bold; \'>nullptr</span><span style=\'color:#800080; \'>;</span>\n        <span style=\'color:#800080; \'>}</span>\n        <span style=\'color:#800000; font-weight:bold; \'>return</span> del<span style=\'color:#800080; \'>;</span>\n    <span style=\'color:#800080; \'>}</span>\n<span style=\'color:#800000; font-weight:bold; \'>public</span><span style=\'color:#e34adc; \'>:</span>\n    <span style=\'color:#603000; \'>vector</span><span style=\'color:#800080; \'>&lt;</span>TreeNode<span style=\'color:#808030; \'>*</span><span style=\'color:#800080; \'>></span> delNodes<span style=\'color:#808030; \'>(</span>TreeNode<span style=\'color:#808030; \'>*</span> root<span style=\'color:#808030; \'>,</span> <span style=\'color:#603000; \'>vector</span><span style=\'color:#800080; \'>&lt;</span><span style=\'color:#800000; font-weight:bold; \'>int</span><span style=\'color:#800080; \'>></span><span style=\'color:#808030; \'>&amp;</span> to_delete<span style=\'color:#808030; \'>)</span> <span style=\'color:#800080; \'>{</span>\n        unordered_set<span style=\'color:#800080; \'>&lt;</span><span style=\'color:#800000; font-weight:bold; \'>int</span><span style=\'color:#800080; \'>></span> <span style=\'color:#603000; \'>set</span><span style=\'color:#808030; \'>(</span>to_delete<span style=\'color:#808030; \'>.</span>begin<span style=\'color:#808030; \'>(</span><span style=\'color:#808030; \'>)</span><span style=\'color:#808030; \'>,</span> to_delete<span style=\'color:#808030; \'>.</span>end<span style=\'color:#808030; \'>(</span><span style=\'color:#808030; \'>)</span><span style=\'color:#808030; \'>)</span><span style=\'color:#800080; \'>;</span>\n        \n        <span style=\'color:#603000; \'>vector</span><span style=\'color:#800080; \'>&lt;</span>TreeNode<span style=\'color:#808030; \'>*</span><span style=\'color:#800080; \'>></span> res<span style=\'color:#800080; \'>;</span>\n        \n        \n        <span style=\'color:#800000; font-weight:bold; \'>bool</span> r <span style=\'color:#808030; \'>=</span> helper<span style=\'color:#808030; \'>(</span>res<span style=\'color:#808030; \'>,</span> root<span style=\'color:#808030; \'>,</span> <span style=\'color:#603000; \'>set</span><span style=\'color:#808030; \'>)</span><span style=\'color:#800080; \'>;</span>\n        <span style=\'color:#800000; font-weight:bold; \'>if</span><span style=\'color:#808030; \'>(</span> <span style=\'color:#808030; \'>!</span>r <span style=\'color:#808030; \'>&amp;</span><span style=\'color:#808030; \'>&amp;</span> root<span style=\'color:#808030; \'>)</span>\n            res<span style=\'color:#808030; \'>.</span>push_back<span style=\'color:#808030; \'>(</span>root<span style=\'color:#808030; \'>)</span><span style=\'color:#800080; \'>;</span>\n        <span style=\'color:#800000; font-weight:bold; \'>return</span> res<span style=\'color:#800080; \'>;</span>\n    <span style=\'color:#800080; \'>}</span>\n<span style=\'color:#800080; \'>}</span><span style=\'color:#800080; \'>;</span>\n</pre>\n<!--Created using ToHtml.com on 2019-09-26 04:33:38 UTC -->
[]