https://github.com/emahtab/delete-nodes-and-return-forest
https://github.com/emahtab/delete-nodes-and-return-forest
depth-first-search forest leetcode
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/emahtab/delete-nodes-and-return-forest
- Owner: eMahtab
- Created: 2021-12-22T18:20:48.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-12-22T18:23:13.000Z (over 3 years ago)
- Last Synced: 2025-02-02T03:26:09.626Z (5 months ago)
- Topics: depth-first-search, forest, leetcode
- Homepage:
- Size: 1.95 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Delete nodes and return forest
## https://leetcode.com/problems/delete-nodes-and-return-forestGiven the root of a binary tree, each node in the tree has a distinct value.
After deleting all nodes with a value in to_delete, we are left with a forest (a disjoint union of trees).
Return the roots of the trees in the remaining forest. You may return the result in any order.
```
Example 1:Input: root = [1,2,3,4,5,6,7], to_delete = [3,5]
Output: [[1,2,null,4],[6],[7]]Example 2:
Input: root = [1,2,4,null,3], to_delete = [3]
Output: [[1,2,4]]
```## Constraints:
```
1. The number of nodes in the given tree is at most 1000.
2. Each node has a distinct value between 1 and 1000.
3. to_delete.length <= 1000
4. to_delete contains distinct values between 1 and 1000.
```# Implementation : O(n)
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List delNodes(TreeNode root, int[] to_delete) {
List result = new ArrayList<>();
if(root == null)
return result;
HashSet toDelete = new HashSet<>();
for (int i : to_delete)
toDelete.add(i);
if(!toDelete.contains(root.val)) {
result.add(root);
}
dfs(root, toDelete, result);
return result;
}
private TreeNode dfs(TreeNode node, Set toDelete, List result) {
if(node == null)
return null;
node.left = dfs(node.left, toDelete, result);
node.right = dfs(node.right, toDelete, result);
if(toDelete.contains(node.val)) {
if(node.left != null) {
result.add(node.left);
}
if(node.right != null) {
result.add(node.right);
}
return null;
}
return node;
}
}
```# References :
https://www.youtube.com/watch?v=aaSFzFfOQ0o