{"id":22513924,"url":"https://github.com/emahtab/delete-nodes-and-return-forest","last_synced_at":"2026-02-09T21:33:24.941Z","repository":{"id":79525408,"uuid":"440948062","full_name":"eMahtab/delete-nodes-and-return-forest","owner":"eMahtab","description":null,"archived":false,"fork":false,"pushed_at":"2021-12-22T18:23:13.000Z","size":2,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-02T03:26:09.626Z","etag":null,"topics":["depth-first-search","forest","leetcode"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-12-22T18:20:48.000Z","updated_at":"2021-12-23T03:18:36.000Z","dependencies_parsed_at":"2023-05-10T17:16:26.735Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/delete-nodes-and-return-forest","commit_stats":{"total_commits":2,"total_committers":1,"mean_commits":2.0,"dds":0.0,"last_synced_commit":"e141b86d2d2557e9823def892f99133f4629d98d"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fdelete-nodes-and-return-forest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fdelete-nodes-and-return-forest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fdelete-nodes-and-return-forest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fdelete-nodes-and-return-forest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/delete-nodes-and-return-forest/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952235,"owners_count":20699452,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["depth-first-search","forest","leetcode"],"created_at":"2024-12-07T03:15:05.060Z","updated_at":"2026-02-09T21:33:19.907Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Delete nodes and return forest\n## https://leetcode.com/problems/delete-nodes-and-return-forest\n\nGiven the root of a binary tree, each node in the tree has a distinct value.\n\nAfter deleting all nodes with a value in to_delete, we are left with a forest (a disjoint union of trees).\n\nReturn the roots of the trees in the remaining forest. You may return the result in any order.\n\n```\nExample 1:\n\nInput: root = [1,2,3,4,5,6,7], to_delete = [3,5]\nOutput: [[1,2,null,4],[6],[7]]\n\nExample 2:\n\nInput: root = [1,2,4,null,3], to_delete = [3]\nOutput: [[1,2,4]]\n```\n\n## Constraints:\n```\n1. The number of nodes in the given tree is at most 1000.\n2. Each node has a distinct value between 1 and 1000.\n3. to_delete.length \u003c= 1000\n4. to_delete contains distinct values between 1 and 1000.\n```\n\n# Implementation : O(n)\n```java\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n    public List\u003cTreeNode\u003e delNodes(TreeNode root, int[] to_delete) {\n        List\u003cTreeNode\u003e result = new ArrayList\u003c\u003e();\n        if(root == null)\n            return result;\n        HashSet\u003cInteger\u003e toDelete = new HashSet\u003c\u003e();\n        for (int i : to_delete) \n            toDelete.add(i);\n        if(!toDelete.contains(root.val)) {\n            result.add(root);\n        }\n        dfs(root, toDelete, result);\n        return result;\n    }\n    \n    private TreeNode dfs(TreeNode node, Set\u003cInteger\u003e toDelete, List\u003cTreeNode\u003e result) {\n        if(node == null)\n            return null;\n        node.left = dfs(node.left, toDelete, result);\n        node.right = dfs(node.right, toDelete, result);\n        if(toDelete.contains(node.val)) {\n            if(node.left != null) {\n                result.add(node.left);\n            }\n            if(node.right != null) {\n                result.add(node.right);\n            }\n            return null;\n        }\n        return node;\n    }\n}\n```\n\n# References :\nhttps://www.youtube.com/watch?v=aaSFzFfOQ0o\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fdelete-nodes-and-return-forest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fdelete-nodes-and-return-forest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fdelete-nodes-and-return-forest/lists"}