1110. Delete Nodes And Return Forest

Delete Nodes And Return Forest (LeetCode 1110) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Postorder Traversal subpattern.

How to solve Delete Nodes And Return Forest

Delete Nodes And Return Forest is a Postorder Traversal problem, part of Tree Traversal Patterns (DFS & BFS). Master postorder traversal for tree height, diameter, and path sum problems. Process children before root.

Work towards the solution in these steps:

  1. Consider using a tree traversal approach to process each node and its children efficiently.
  2. Think about performing a postorder traversal so that you handle child nodes before deciding what to do with the current node.
  3. While traversing, keep track of nodes to delete and consider how removing a node affects its children—specifically, whether children become new roots in the forest.

The reference solution runs in O(N) time and O(N) space.

The Delete Nodes And Return Forest solution and editorial walks through the full approach with reference code.

Problem statement

Given the root of a binary tree, each node in the tree has a distinct value.

After deleting all nodes with a value in todelete, 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], todelete = [3,5] Output: [[1,2,null,4],[6],[7]]

Example 2:

Input: root = [1,2,4,null,3], todelete = [3] Output: [[1,2,4]]

Constraints:

- The number of nodes in the given tree is at most 1000.

- Each node has a distinct value between 1 and 1000.

- todelete.length <= 1000

- todelete contains distinct values between 1 and 1000.

Topics and companies

Topics: Array, Hash Table, Tree, Depth-First Search, Binary Tree.

Reported in interviews at Google, Amazon, Facebook, Salesforce.

How to practise Delete Nodes And Return Forest on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(N) time and O(N) space.

Open Delete Nodes And Return Forest in the code editor, or read the Delete Nodes And Return Forest editorial for a worked solution with its approach and complexity analysis.

Where Delete Nodes And Return Forest sits in the DSA pattern sheet

Problems related to Delete Nodes And Return Forest

Other problems that use the same Recursive Postorder Traversal and Tree Traversal Patterns (DFS & BFS) techniques:

Delete Nodes And Return Forest FAQ

What is the LeetCode number of Delete Nodes And Return Forest?

Delete Nodes And Return Forest is LeetCode problem 1110, rated Medium.

What pattern is used to solve Delete Nodes And Return Forest?

Delete Nodes And Return Forest is a Postorder Traversal problem. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Postorder Traversal subpattern.

What is the time complexity of Delete Nodes And Return Forest?

The reference solution runs in O(N) time and O(N) space.

Which companies ask Delete Nodes And Return Forest?

Delete Nodes And Return Forest has been reported in interviews at Google, Amazon, Facebook, Salesforce.

Preparing this workspace.