-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeleteNodesAndReturnForest.java
68 lines (56 loc) · 1.5 KB
/
DeleteNodesAndReturnForest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package binarytree;
import java.util.*;
import binarytree.BinaryTreeTest.TreeNode;
/**
* @author Shogo Akiyama
* Solved on 01/09/2020
*
* 1110. Delete Nodes And Return Forest
* https://leetcode.com/problems/delete-nodes-and-return-forest/
* Difficulty: Medium
*
* Approach: Recursion
* Runtime: 2 ms, faster than 97.35% of Java online submissions for Delete Nodes And Return Forest.
* Memory Usage: 43.2 MB, less than 100.00% of Java online submissions for Delete Nodes And Return Forest.
*
* Time Complexity: O(n)
* Space Complexity: O(d) for the recursive stack
* Where n is the number of nodes in a tree and d is the maximum depth of the tree
*
* @see BinaryTreeTest#testHouseRobberIII()
*/
public class DeleteNodesAndReturnForest {
private Set<Integer> toDelete;
private List<TreeNode> roots;
public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
toDelete = new HashSet<Integer>();
roots = new ArrayList<TreeNode>();
for (int d : to_delete) {
toDelete.add(d);
}
TreeNode newRoot = delete(root);
if (newRoot != null) {
roots.add(newRoot);
}
return roots;
}
private TreeNode delete(TreeNode root) {
if (root == null) {
return null;
}
if (toDelete.contains(root.val)) {
TreeNode left = delete(root.left);
if (left != null) {
roots.add(left);
}
TreeNode right = delete(root.right);
if (right != null) {
roots.add(right);
}
return null;
}
root.left = delete(root.left);
root.right = delete(root.right);
return root;
}
}