LeetCode 1110: Delete Nodes And Return Forest Solution

Master LeetCode problem 1110 (Delete Nodes And Return Forest), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1110. Delete Nodes And Return Forest

Problem Explanation

Explanation

To solve this problem, we can perform a depth-first search (DFS) traversal of the binary tree. At each node, we check if the node's value is in the to_delete list. If it is, we mark the node for deletion and recursively process its left and right child nodes. If the current node itself is not in the to_delete list but its parent is, we add the current node to the result as a new tree root. We continue this process until we have traversed all nodes of the tree.

Algorithm:

  1. Initialize an empty list to store the result forest.
  2. Perform a recursive DFS traversal of the tree.
  3. At each node:
    • Check if the node's value is in the to_delete list.
    • If it is, mark the node for deletion and recursively process its left and right child nodes.
    • If the node is not marked for deletion but its parent is, add the node to the result as a new tree root.
  4. Return the list of root nodes of the remaining trees in the forest.

Time Complexity:

The time complexity of this approach is O(N), where N is the number of nodes in the binary tree.

Space Complexity:

The space complexity is also O(N) due to the recursive calls on the call stack and the space required to store the result forest.

Solution Code

class Solution {
    public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
        List<TreeNode> result = new ArrayList<>();
        Set<Integer> toDeleteSet = new HashSet<>();
        for (int val : to_delete) {
            toDeleteSet.add(val);
        }
        dfs(root, toDeleteSet, result, true);
        return result;
    }
    
    private TreeNode dfs(TreeNode node, Set<Integer> toDeleteSet, List<TreeNode> result, boolean isRoot) {
        if (node == null) return null;
        
        boolean deleted = toDeleteSet.contains(node.val);
        if (isRoot && !deleted) {
            result.add(node);
        }
        
        node.left = dfs(node.left, toDeleteSet, result, deleted);
        node.right = dfs(node.right, toDeleteSet, result, deleted);
        
        return deleted ? null : node;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1110 (Delete Nodes And Return Forest)?

This page provides optimized solutions for LeetCode problem 1110 (Delete Nodes And Return Forest) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1110 (Delete Nodes And Return Forest)?

The time complexity for LeetCode 1110 (Delete Nodes And Return Forest) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1110 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1110 in Java, C++, or Python.

Back to LeetCode Solutions