Postorder Traversal | C++, Python, Java
Approach\n Describe your approach to solving the problem. \nWe can perform a Depth-First Search on the tree while also using a Hashset to optimize looking up no
# Approach\n<!-- Describe your approach to solving the problem. -->\nWe can perform a Depth-First Search on the tree while also using a Hashset to optimize looking up node values in `to_delete`. \n\n- Recursively perform a Postorder Traversal on `root`. `flag` indicates whether the current node is a root of a tree in the forest. In each call we set flag to be `true` if the current node\'s parent was to be deleted (making the current node a root if it\'s not to be deleted).\n- After the recursive calls, if the current node is not in `to_delete` and `flag` is `true`, then this is the root of one of the new trees and can be added to `res`. Moreover, if the current node needs to be deleted, set its value to `null`.\n- Before starting the DFS, add all the elements of `to_delete` to a Hashset to improve the lookup time for node values. Furthermore, we initially have `flag = true` for the first call to indicate that the root is a potential root of a tree in the forest.\n# Complexity\n- Time complexity: $$O(n+m)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n+m)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C++ []\nclass Solution {\nprivate:\n    bool set[1001] = {};\n    void dfs(TreeNode* &root, bool flag, vector<TreeNode*>& res){\n        if (root == nullptr) return;\n        dfs(root->left, set[root->val], res);\n        dfs(root->right, set[root->val], res);\n        if (!set[root->val] && flag) res.push_back(root);\n        if (set[root->val]) root = nullptr;\n    }\npublic:\n    vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {\n        vector<TreeNode*> res;\n        for (int num : to_delete)\n            set[num] = true;\n        dfs(root, true, res);\n        return res;\n    }\n};\n```\n```python []\nclass Solution:\n    def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]:\n        s = set(to_delete)\n        res = []\n        def dfs(root, flag):\n            if not root: return None\n            toDelete = (root.val in s)\n            root.left = dfs(root.left, toDelete)\n            root.right = dfs(root.right, toDelete)\n            if not toDelete and flag: res.append(root)\n            return None if toDelete else root\n        dfs(root, True)\n        return res\n        \n```\n```Java []\nclass Solution {\n    private boolean[] set = new boolean[1001];\n    private List<TreeNode> res;\n    private TreeNode dfs(TreeNode root, boolean flag){\n        if (root == null) return root;\n        root.left = dfs(root.left, set[root.val]);\n        root.right = dfs(root.right, set[root.val]);\n        if (!set[root.val] && flag) res.add(root);\n        return set[root.val] ? null : root;\n    }\n    public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {\n        res = new ArrayList<>();\n        for (int n : to_delete)\n            set[n] = true;\n        dfs(root, true);\n        return res;\n    }\n}\n```\n*upvote pls* \uD83D\uDE18\n
['Array', 'Hash Table', 'Tree', 'Depth-First Search', 'C', 'Binary Tree', 'Python', 'C++', 'Java', 'Python3']