Problem
Given 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.
Examples
Example 1:
graph TD; 1; 1 --- 2; 1 --- 3; 2 --- 4; 2 --- 5; 3 --- 6; 3 --- 7;
|
|
Example 2:
|
|
Solution
Method 1 - Recursion
We can use recursive traversal to do 2 things:
- Deletion: Decide during traversal whether the current node needs to be deleted.
- Forest Formation:
- If a node is marked for deletion, its children should become new roots of the resulting forest if they exist.
- If a node isn’t marked for deletion and it’s the root, add it to the result list.
So, here are the steps we can follow:
- Convert the
to_delete
list into a set for O(1) average-time complexity look-ups. - Define a recursive function to traverse the tree:
- If the current node is
null
, returnnull
. - Recursively call the function on left and right children.
- If the current node is in the
to_delete
set:- Recursively handle its left and right children to add them to the forest if they are not
null
. - Return
null
for the current node to remove it.
- Recursively handle its left and right children to add them to the forest if they are not
- Otherwise, return the current node.
- If the current node is
- Handle the Root: Check if the root itself is to be deleted or retained.
Here is the video explanation of the same:
Code
|
|
Complexity
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(h)
assuming height of tree